【发布时间】:2021-02-15 00:24:00
【问题描述】:
考虑:
#include <compare>
template<class=void>
constexpr int f() { return 1; }
unsigned int x;
using T = decltype(x <=> f());
GCC 和 MSVC 接受 T 的声明。 Clang 拒绝它,并显示以下错误消息:
<source>:7:26: error: argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int' using T = decltype(x <=> f()); ^ 1 error generated.
如果模板头 (template<class=void>) 被删除,或者如果 f 在声明 T 之前被显式或隐式实例化,则 Clang 接受它。例如,Clang 接受:
#include <compare>
template<class=void>
constexpr int f() { return 1; }
unsigned x;
auto _ = x <=> f();
using T = decltype(x <=> f());
哪个编译器是正确的,为什么?
【问题讨论】:
标签: c++ language-lawyer constexpr c++20 spaceship-operator