c++ - VS19 上的嵌套模板 (C++17) 编译失败
问题描述
代码:
(请注意,模板未实例化 - 这不是错误,即使没有它,编译也会失败) 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 社区 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 提交错误报告。
推荐阅读
- python-3.x - Python 抑制特定的输出消息
- c++ - C++ Sockets 多个客户端的连接结构
- ruby-on-rails - rails 服务器无法启动 - `initialize': JSON 文本必须至少包含两个八位字节!(JSON::ParserError)
- javascript - 在 JavaScript 中捕获嵌套表达式
- lua - Lua在子进程后返回输出
- reactjs - 用子菜单反应原生底部抽屉导航?
- r - 创建连接子字符串的新列
- database - 从服务器中的数据库备份创建本地 postgreSQL 数据库
- powershell - 使用 NSSM 将 PS1(调用 FileSystemWatcher 类)转换为服务
- sql - 如何将现有表中的 7000 条记录中的前 1000 条记录复制到 postgreSql 中的其他新表