简体   繁体   English

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

[英]Nested template (C++17) compilation fails on VS19

Code:代码:

(Note that template is not instantiated - that's not a mistake, compilation fails even without it) main.cpp: (请注意,模板未实例化 - 这不是错误,即使没有它,编译也会失败) 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 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) : compiles without warnings GCC (Msys64 MinGW64) :编译时没有警告

Visual Studio Comminity 2019: 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.

Printed by MSVC error contains identical signatures for both declaration and definition, but it insists that they do not match.由 MSVC 打印的错误包含声明和定义的相同签名,但它坚持认为它们不匹配。

I'm compiling on Windows 10, using CMake (CLion) and default compiler options (only specifying C++17 standard level)我正在 Windows 10 上编译,使用 CMake (CLion) 和默认编译器选项(仅指定 C++17 标准级别)

It seems msvc has issues with the template .似乎 msvc 的template有问题。 This compiles这编译

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>();
}

As stated in the comments, the template might be required by the standard.如评论中所述,标准可能需要模板。 In that case, maybe you should file a bug report at Microsoft.在这种情况下,也许您应该向 Microsoft 提交错误报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM