C++ 是否定义了以下代码的行为:
template <class T>
concept C = true;
struct C {};
template <C T>
struct A {};
template <struct C T>
struct B {};
int main() {
// when using MSVC
C c; // error
bool b = C<int>; // also error
// and can't use concept C anywhere other
// than the template parameter list
constexpr struct C cc {};
struct C c2; // ok
A<int> a; // ok
A<cc> a; // error
B<cc> b; // ok
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谁能回答我:
template <struct C T>
struct B {};
Run Code Online (Sandbox Code Playgroud)
我在 cppreference 上找到了这个:
当用作模板参数时,类 T 是名为 T 的类型模板参数,而不是其类型 T 由详细类型说明符引入的未命名非类型参数
如果您有任何模板(除了具有重载的函数模板之外),则在同一范围内不能有任何其他具有相同名称的模板。
因此,这意味着您的声明template <class T> concept C意味着您不能拥有名为 的其他实体::C。这被定义为格式错误,因此 C++ 标准表示该代码不应编译。
如果通过内联名称空间引入问题,则会出现您的问题:
template <class T>
concept C = true;
inline namespace x {
struct C {};
}
Run Code Online (Sandbox Code Playgroud)
现在,struct C将引用 struct x::C,并且没有办法明确地引用该概念。