llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[libcxx] Ambiguous Reference to std::pair in SPEC2006 447.dealII Example When Using libc++

Open andreww0210 opened this issue 1 year ago • 2 comments

When compiling the code from the SPEC2006 447.dealII benchmark with libc++, an ambiguous reference to std::pair occurs. The problematic code like this:

#include <utility>
namespace std {
template <typename T1, typename T2> struct pair;
} // namespace std

std::pair<unsigned, unsigned> test() { return std::make_pair(1u, 2u); }

The error message is:

test.cpp:6:6: error: reference to 'pair' is ambiguous
    6 | std::pair<unsigned, unsigned> test() { return std::make_pair(1u, 2u); }
      |      ^
../riscv64-unknown-elf/include/c++/v1/__utility/pair.h:79:29: note: candidate found by name lookup is 'std::__1::pair'
   79 | struct _LIBCPP_TEMPLATE_VIS pair
      |                             ^
test.cpp:3:44: note: candidate found by name lookup is 'std::pair'
    3 | template <typename T1, typename T2> struct pair;
      |                                            ^
1 error generated.

Is there a recommended approach to resolve this issue without modifying the SPEC2006 source code? Could libc++ provide a mechanism to handle such forward declarations gracefully?

andreww0210 avatar Mar 20 '25 10:03 andreww0210

This program violates [namespace.std], so it has undefined behavior (or is ill-formed, no diagnostic required per P3534R0).

I don't think libc++ can accept this example. It's possible to directly provide pair in std but not in any nested inline namespace, but this will break ABI with nearly zero value.

frederick-vs-ja avatar Mar 20 '25 14:03 frederick-vs-ja

The recommended approach is to fix SPEC2006. As @frederick-vs-ja pointed our this code violates the "Constraints on programs". It would be interesting why they think this is needed.

mordante avatar Mar 20 '25 18:03 mordante