我不确定是我理解错了
requires
还是clang有bug。这是代码:
#include <cassert>
#include <concepts>
template<std::unsigned_integral T>
constexpr unsigned int
foo(T x)
{
return x;
}
template<std::unsigned_integral T> requires (sizeof(T) == 1)
constexpr unsigned int
foo(T x)
{
return x + 1;
}
int
main()
{
assert(foo(static_cast<unsigned char>(0)) == 1);
unsigned int (*function)(unsigned char x) = foo;
assert(function(0) == 1);
}
clang -v
说
Apple clang version 16.0.0 (clang-1600.0.26.6)
Target: arm64-apple-darwin24.2.0
并编译并运行它,因为
clang -std=c++20 file.cc -o file; file
产生
Assertion failed: (function(0) == 1), function main, file file.cc, line 24.
Abort trap: 6
这让我很困惑:为什么编译器会选择不同的实例化?在这两种情况下,我都希望打电话给
unsigned int foo<unsigned char> (unsigned char x)
,但也许我误解了应该发生的事情。