<可变参数模板> Visual Studio 中具有默认值编译错误的模板参数

<variadic template> template parameters with default value compilation error in Visual Studio

本文关键字:参数 默认值 编译 错误 gt lt 变参 Visual Studio      更新时间:2023-10-16

在将代码从GCC移植到MSVC时遇到了这个晦涩的问题。

请考虑以下代码片段:

template <typename T>
struct Foo;
template <template <typename...> typename Container, typename Arg>
struct Foo<Container<Arg>> {
using arg_t = Arg;
};
template <typename X>
struct A {};
template <typename X, typename Y = void>
struct B {};
template <typename X, typename Y = void, typename Z = void>
struct C {};
int main() {
typename Foo<A<int>>::arg_t a;
typename Foo<B<int>>::arg_t b;
typename Foo<C<int>>::arg_t c;
return 0;
}

我们使用Foo特征来提取模板类的第一个参数,其中从第二个模板参数开始具有默认值(例如,实际用例是std::unique_ptr)。Clang和GCC完美地处理了这个片段,但是MSVC(Visual Studio 17附带的那个)抛出了非常不明显的编译错误。

事实证明,GCC 和 Clang 以某种方式处理默认模板参数,以便<template <typename...> typename Bar, typename X> Bar<X>接口接受A<X, Y=void>。另一方面,MSVC 没有。不确定它是标准扩展还是只是GCC/Clang扩展。 无论如何,解决方案是添加虚拟可变参数以匹配剩余参数

template <typename T>
struct Foo;
template <template <typename...> typename Container, 
typename Arg, typename... MsvcWorkaround>
struct Foo<Container<Arg, MsvcWorkaround....>> {
using arg_t = Arg;
};
template <typename X>
struct A {};
template <typename X, typename Y = void>
struct B {};
template <typename X, typename Y = void, typename Z = void>
struct C {};
int main() {
typename Foo<A<int>>::arg_t a;
typename Foo<B<int>>::arg_t b;
typename Foo<C<int>>::arg_t c;
return 0;
}

从编译器错误中理解问题真的很难,我无法用谷歌搜索出解决方案,这就是我想分享我的原因。