gpt4 book ai didi

c++ - 注入(inject)的类名作为类型

转载 作者:IT老高 更新时间:2023-10-28 22:38:49 38 4
gpt4 key购买 nike

给定以下代码,

template <class> using void_t = void;
template <class C, class = void> struct X { enum { v = 0 }; };
template <class C> struct X<C, void_t<typename C::T> > { enum { v = 1 }; };
struct T { };
int main() { return X<T>::v; }

main 应该返回什么? GCC 和 MSVC 说 1,Clang 说 0。

最佳答案

我认为 Clang 就在这里。 [class.qual] 中的规则是:

In a lookup in which function names are not ignored and the nested-name-specifier nominates a class C:

  • if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C ([class]), or
  • [... irrelevant here ...]

the name is instead considered to name the constructor of class C. [ Note: For example, the constructor is not an acceptable lookup result in an elaborated-type-specifier so the constructor would not be used in place of the injected-class-name. — end note ] Such a constructor name shall be used only in the declarator-id of a declaration that names a constructor or in a using-declaration. [ Example:

struct A { A(); };
struct B: public A { B(); };

A::A() { }
B::B() { }

B::A ba; // object of type A
A::A a; // error, A​::​A is not a type name
struct A::A a2; // object of type A

— end example ]

typename C::TA::A 是同一种东西,它是查找不忽略函数名(typename 不会导致函数名被忽略)。所以,在typename C::T中,当CT时,名称T被认为是命名构造函数。由于它不是类型名称,我们应该得到替换失败并回退到主模板。

归档 86818 .

关于c++ - 注入(inject)的类名作为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51621560/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com