下面是演示各种参数包扩展方案的代码。失败的方案是在模板中的函数中扩展static const
成员以生成类型:using type = intco<funcplus(v_values::value...)>
。 (但是,类似的情况,其中扩展的结果定义了一个整数常量而不是一个类型成功,如下所示。)
代码使用Clang(Apple LLVM版本8.0.0(clang-800.0.42.1))和使用GCC(gcc(Ubuntu 6.2.0-5ubuntu12)6.2.0 20161005)成功编译但失败当使用MSVC(Microsoft Visual Studio Community 2015,v14.0.2531.01 update 3)时,警告&#34;依赖名称不是类型&#34;然后是错误&#34;语法错误:&#39; ...&#39;&#34;。
MSVC是否有一些编译器标志可以解决(或可能导致)此问题?
template<int t_value>
struct intco {
static const int value = t_value;
};
template<int t_head, int... v_body>
struct structplus {
static const int value = t_head + structplus<v_body...>::value;
};
template<int t_head>
struct structplus<t_head> {
static const int value = t_head;
};
template<typename... v_values>
struct intco_structplus {
//static const int value = structplus<v_values::value...>::value; //good
using type = structplus<v_values::value...>; //good
static const int value = type::value; //good
};
static_assert(6 == intco_structplus<intco<1>, intco<2>, intco<3>>::value, "1 + 2 + 3 ?= 6");
template<typename... v_ints>
constexpr int funcplus(int head, v_ints... v_body) {
return head + funcplus(v_body...);
}
template<>
constexpr int funcplus<>(int head) {
return head;
}
template<typename... v_values>
struct intco_funcplus {
//static const int value = funcplus(v_values::value...); //good
//static const int value = intco<funcplus(v_values::value...)>::value; //good
using type = intco<funcplus(v_values::value...)>; //"dependent name is not a type"
static const int value = type::value;
};
static_assert(6 == intco_funcplus<intco<1>, intco<2>, intco<3>>::value, "1 + 2 + 3 ?= 6");