C++ C++;类名注入

C++ C++;类名注入,c++,c++11,language-lawyer,C++,C++11,Language Lawyer,根据标准[class]/2: …类名也被插入到类本身的作用域中;这称为注入的类名 此外,[basic.scope.pdecl]/9: 注入类名称的声明点(第9条)紧跟在类定义的大括号之后 最后,[basic.lookup.classref]/3及其示例: 如果非限定id为~type name,则会查找类型名称 到目前为止,我们可以收集到: 在类a的作用域中,存在一个名称a 该名称在类A的定义括号的开头声明 这个名字命名了一个类型 如果上述内容正确,那么为什么以下代码无法编译(在MSVC2015中

根据标准[class]/2:

…类名也被插入到类本身的作用域中;这称为注入的类名

此外,[basic.scope.pdecl]/9:

注入类名称的声明点(第9条)紧跟在类定义的大括号之后

最后,[basic.lookup.classref]/3及其示例:

如果非限定id为~type name,则会查找类型名称

到目前为止,我们可以收集到:

  • 在类a的作用域中,存在一个名称a
  • 该名称在类A的定义括号的开头声明
  • 这个名字命名了一个类型
  • 如果上述内容正确,那么为什么以下代码无法编译(在MSVC2015中):

    错误消息

    错误C2039“{ctor}”:不是“inj”的成员

    似乎与标准不符:

    注意:例如,构造函数在详细的类型说明符中不是可接受的查找结果,因此不会在适当的位置使用构造函数 注入的类名的名称-尾注


    由于以下代码可以在其他编译器中正确编译和运行,因此它是MSVC2015中的一个错误

    #include <boost/type_index.hpp>
    #include <iostream>
    
    struct inj
    {
        int g;
    };
    
    typedef struct inj::inj inj2;
    
    int main()
    {
    
        inj2 ii;
        std::cout << boost::typeindex::type_id_with_cvr<decltype(ii)>().pretty_name() << '\n';
    }
    
    #包括
    #包括
    结构输入
    {
    int g;
    };
    typedef结构inj::inj inj2;
    int main()
    {
    inj2 ii;
    
    Std::CUT,因为它不是合法的C++,可能是哪个段落是注释?可能只是另一个MSVC bug。G++和CLAN+++都接受这个例子。@ SP2DANY[Cal.Qual] P2在标准本身(3.4.3.1)中有一个几乎相同的代码示例,MSVC无法编译它,因此它最好包含在bug报告中。
    struct inj
    {};
    
    typedef struct inj::inj inj2;
    
    #include <boost/type_index.hpp>
    #include <iostream>
    
    struct inj
    {
        int g;
    };
    
    typedef struct inj::inj inj2;
    
    int main()
    {
    
        inj2 ii;
        std::cout << boost::typeindex::type_id_with_cvr<decltype(ii)>().pretty_name() << '\n';
    }