root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.54k stars 1.23k forks source link

Cannot use range-v3 in the interpreter with C++20 enabled #13001

Open pikacic opened 1 year ago

pikacic commented 1 year ago

Check duplicate issues.

Describe the bug

Because of the implicit using namespace std in the interpreter, trying to use range-v3 headers result in errors about ambiguous references, like:

In file included from ROOT_prompt_0:1:
In file included from /cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/range/traits.hpp:24:
In file included from /cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/range_fwd.hpp:23:
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/concepts/compare.hpp:35:39: error: reference to 'ranges' is ambiguous
        concept compares_as = same_as<ranges::common_comparison_category_t<T, Cat>, Cat>;
                                      ^
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/compare.hpp:25:11: note: candidate found by name lookup is 'ranges'
namespace ranges
          ^
/cvmfs/sft.cern.ch/lcg/releases/gcc/13.1.0-b3d18/x86_64-el9/include/c++/13.1.0/experimental/string_view:689:13: note: candidate found by name lookup is 'std::ranges'
  namespace ranges
            ^

What is the expected behaviour?

I expect to be able to use the top level ranges namespace at the same time as std::ranges.

How to reproduce?

From lxplus9.cern.ch

❯ . /cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/setup.sh
❯ root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.28/05                        https://root.cern |
  | (c) 1995-2023, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jun 10 2023, 00:28:00                 |
  | From heads/v6-28-00-patches@v6-28-04-94-gb7de1efc73              |
  | With g++ (GCC) 13.1.0                                            |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] #include <range/v3/range/traits.hpp>
In file included from ROOT_prompt_0:1:
In file included from /cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/range/traits.hpp:24:
In file included from /cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/range_fwd.hpp:23:
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/concepts/compare.hpp:35:39: error: reference to 'ranges' is ambiguous
        concept compares_as = same_as<ranges::common_comparison_category_t<T, Cat>, Cat>;
                                      ^
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/range/v3/compare.hpp:25:11: note: candidate found by name lookup is 'ranges'
namespace ranges
          ^
/cvmfs/sft.cern.ch/lcg/releases/gcc/13.1.0-b3d18/x86_64-el9/include/c++/13.1.0/experimental/string_view:689:13: note: candidate found by name lookup is 'std::ranges'
  namespace ranges
            ^

ROOT version

v6-28-00-patches

How did you install ROOT?

LCG nightly builds in /cvmfs/sft-nightlies.cern.ch

Which operating system are you using?

Linux Alma 9

Additional context

I wanted to report this problem as a compelling use case that requires #11027

pcanal commented 1 year ago

As a side note, the error mentions:

/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4/Mon/x86_64-el9-gcc13-dbg/include/concepts/compare.hpp:35:39: error: reference to 'ranges' is ambiguous
        concept compares_as = same_as<ranges::common_comparison_category_t<T, Cat>, Cat>;
                                      ^

Shouldn't this header file also be improved by fully qualifying ranges here? (i.e. would the same problem happens in a compilation unit where the user of this header and string_view want to use using namespace std;?

pikacic commented 1 year ago

Yes, of course, I can reproduce the error in regular C++ with something as simple as https://godbolt.org/z/rnqWer3bj:

#include <ranges>
using namespace std;
#include <range/v3/views.hpp>

but I see 2 errors in this example:

The following code works:

#include <ranges>
#include <range/v3/views.hpp>
using namespace std;

still a bit silly, but it works.

So, I'm sorry, but I do not buy the comment that developers of libraries should always put :: in front of every top level namespace. I believe it is fairy reasonable to assume that nobody does using namespace std; before including any header.

pcanal commented 1 year ago

Yes, it is still true that we need to remove/replace the using namespace (my note was just a side note and was not meant to undermine this report) and it is also true that making one's own headers more resilient to silly but legal usage is usually worth the slight (and debatable) decrease in readability (by having/adding more qualification).