如何为模板别名指定模板推导指南?

Rah*_*obe 5 c++ template-argument-deduction c++17 visual-studio-2019

我正在尝试使此代码VS 2019 (16.10.4)/std:c++17. 以下失败:

namespace my
{
   template<class _Key, class _Compare = std::less<_Key>, class _Allocator = std::allocator<_Key>>
   using Set = std::set<_Key, _Compare, _Allocator>;
}

void test()
{
    std::set set1 = { 1, 2, 3 };
    static_assert(std::is_same_v<std::set<int>, decltype(set1)>);

    my::Set set2 = { 1, 2, 3 }; // Error here.
    static_assert(std::is_same_v<my::Set<int>, decltype(set2)>);
}
Run Code Online (Sandbox Code Playgroud)

有错误:

error C2955: 'my::Set': use of alias template requires template argument list
message : see declaration of 'my::Set'
Run Code Online (Sandbox Code Playgroud)

相同的代码在 godbolt 上编译良好,gcc -std=c++17无需任何推导指南。请参阅示例

有没有办法让它VS 2019 (16.10.4)编译/std:c++17?我的实际代码非常冗长。我创建了这个最小的可重复样本,它可能只是解决这个小问题。我也尝试指定推导指南,但my::Set它们也有类似的错误。

Fed*_*dor 0

my::Set您的代码中是别名模板,别名模板的类模板参数推导是 C++20 的新功能:https ://en.cppreference.com/w/cpp/compiler_support/20

通过实验可以发现,本例中的模板参数推导在 Visual Studio 16.11 版本之后就可以使用,请同时指定/std:c++20命令行选项。演示: https: //gcc.godbolt.org/z/93jdvfPcn

相同的代码可以在 godbolt 上使用 gcc -std=c++17 编译良好,无需任何推导指南。

这是 GCC 中的一个错误,它允许在 C++17 模式下对别名模板进行类模板参数推导:https://gcc.gnu.org/bugzilla/show_bug.cgi ?id=103852