代码:
(请注意,模板未实例化-这不是一个错误,即使没有模板,编译也会失败)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...>();
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(msvc_exploit001)
set(CMAKE_CXX_STANDARD 17)
add_library(msvc_exploit001 main.cpp)
GCC(Msys64 MinGW64):编译时无警告
Visual Studio Comminity 2019:
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打印的错误包含用于声明和定义的相同签名,但它坚持认为它们不匹配。
我正在Windows 10上编译,使用CMake(CLion)和默认编译器选项(仅指定C ++ 17标准级别)
看来msvc与template
。这样编译
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>();
}
如评论中所述,标准可能需要模板。在这种情况下,也许您应该向Microsoft提交错误报告。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句