C++ VS19上的嵌套模板(C+;+;17)编译失败

C++ VS19上的嵌套模板(C+;+;17)编译失败,c++,templates,gcc,visual-c++,variadic-templates,C++,Templates,Gcc,Visual C++,Variadic Templates,代码: (注意,模板没有实例化——这不是一个错误,即使没有它,编译也会失败) main.cpp: namespace mylib { template<typename ...Params> class SomeClass { public: template<typename ...NestedParams> class NestedClass { };

代码:

(注意,模板没有实例化——这不是一个错误,即使没有它,编译也会失败) main.cpp:

namespace mylib {
    template<typename ...Params>
    class SomeClass {
        public:
            template<typename ...NestedParams>
            class NestedClass {
            };

            template<typename ...NestedParams>
            NestedClass<NestedParams...> createNestedInstance();
    };
}

template<typename... Params>
template<typename... NestedParams>
typename mylib::SomeClass<Params...>::template NestedClass<NestedParams...>
mylib::SomeClass<Params...>::createNestedInstance() {
    return SomeClass::NestedClass<NestedParams...>();
}
GCC(Msys64-MinGW64): 编译时没有警告

2019年Visual Studio委员会:

msvc_exploit001.cpp(25): error C2244: 'mylib::SomeClass<Params...>::createNestedInstance': unable to match function definition to an existing declaration
msvc_exploit001.cpp(25): note: see declaration of 'mylib::SomeClass<Params...>::createNestedInstance'
msvc_exploit001.cpp(25): note: definition
msvc_exploit001.cpp(25): note: 'mylib::SomeClass<Params...>::NestedClass<NestedParams...> mylib::SomeClass<Params...>::createNestedInstance(void)'
msvc_exploit001.cpp(25): note: existing declarations
msvc_exploit001.cpp(25): note: 'mylib::SomeClass<Params...>::NestedClass<NestedParams...> mylib::SomeClass<Params...>::createNestedInstance(void)'
NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~4\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
msvc_exploit001.cpp(25):错误C2244:“mylib::SomeClass::createNestedInstance”:无法将函数定义与现有声明匹配
msvc_exploit001.cpp(25):注意:请参见“mylib::SomeClass::createNestedInstance”的声明
msvc_001.cpp(25):注:定义
msvc_exploit001.cpp(25):注意:'mylib::SomeClass::NestedClass mylib::SomeClass::createNestedInstance(void)'
msvc_001.cpp(25):注:现有声明
msvc_exploit001.cpp(25):注意:'mylib::SomeClass::NestedClass mylib::SomeClass::createNestedInstance(void)'
NMAKE:致命错误U1077:'C:\PROGRA~2\MICROS~4\2019\common~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\cl.exe':返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\NMAKE.exe”:返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\NMAKE.exe”:返回代码“0x2”
停止
NMAKE:致命错误U1077:““C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\NMAKE.exe”:返回代码“0x2”
停止
由MSVC打印的错误包含声明和定义的相同签名,但它坚持认为它们不匹配


我在Windows 10上编译,使用CMake(CLion)和默认编译器选项(仅指定C++17标准级别)

msvc似乎在
模板上有问题。这就编译了

namespace mylib {
    template<typename ...Params>
    class SomeClass {
        public:
            template<typename ...NestedParams>
            class NestedClass {
            };

            template<typename ...NestedParams>
            NestedClass<NestedParams...> createNestedInstance();
    };
}

template<typename... Params>
template<typename... NestedParams>
typename mylib::SomeClass<Params...>::NestedClass<NestedParams...>
mylib::SomeClass<Params...>::createNestedInstance() {
    return SomeClass::NestedClass<NestedParams...>();
}

int main() {
    auto sc = mylib::SomeClass<int, bool>();
    auto c = sc.createNestedInstance<float, double>();
}
名称空间mylib{
模板
上课{
公众:
模板
类嵌套类{
};
模板
NestedClass创建NestedInstance();
};
}
模板
模板
typename mylib::SomeClass::NestedClass
mylib::SomeClass::createNestedInstance(){
返回SomeClass::NestedClass();
}
int main(){
auto sc=mylib::SomeClass();
自动c=sc.createNestedInstance();
}

如评论中所述,本标准可能需要该模板。在这种情况下,也许您应该向Microsoft提交一份错误报告。

您能给出一个答案吗?如果您从
createdNestedInstance
方法声明中删除不需要的显式
SomeClass::
,是否仍然会出现编译错误?@SamVarshavchik是的,没有它仍然会出现错误。(实际上,它最初没有它——我后来添加了它)。把它移走了sources@JHBonarius只是因为这个问题是复制没有实例化。将
add_executable
替换为
add_library
以实现完美匹配。我打开了您使用的编译器?@kkolyan Visual Studio 2015 14.0标准要求使用的
模板
关键字。但是是的,删除它确实会让MSVC接受代码(而clang++会拒绝它)。
namespace mylib {
    template<typename ...Params>
    class SomeClass {
        public:
            template<typename ...NestedParams>
            class NestedClass {
            };

            template<typename ...NestedParams>
            NestedClass<NestedParams...> createNestedInstance();
    };
}

template<typename... Params>
template<typename... NestedParams>
typename mylib::SomeClass<Params...>::NestedClass<NestedParams...>
mylib::SomeClass<Params...>::createNestedInstance() {
    return SomeClass::NestedClass<NestedParams...>();
}

int main() {
    auto sc = mylib::SomeClass<int, bool>();
    auto c = sc.createNestedInstance<float, double>();
}