无法使用可变参数编译 constexpr 函数
Unable to compile constexpr function with variadic arguments
我正在编写一个带有可变参数的 constexpr
乘积函数。我只能在下面使用“版本 1”。尝试编译“版本 2”时,出现错误 声明类型包含未扩展的参数包 'data_type'。谁能解释为什么会这样?
/** Version 1. */
template <typename data_type, typename ...data_types>
constexpr data_type Product1(data_type _first, data_types ..._rest) // This code compiles :)
{
return _first*(_rest * ...);
}
/** Version 2. */
template <typename ...data_type>
constexpr data_type Product2(data_type ..._rest) // This code does not compile :(
{
return (_rest * ...);
}
将return类型改为auto,数据类型为pack,不能作为return类型使用
template <typename ...data_type>
constexpr auto Product2(const data_type& ..._rest)
{
return (_rest * ...);
}
int main()
{
constexpr auto p = Product2(1, 2, 3);
return 0;
}
我正在编写一个带有可变参数的 constexpr
乘积函数。我只能在下面使用“版本 1”。尝试编译“版本 2”时,出现错误 声明类型包含未扩展的参数包 'data_type'。谁能解释为什么会这样?
/** Version 1. */
template <typename data_type, typename ...data_types>
constexpr data_type Product1(data_type _first, data_types ..._rest) // This code compiles :)
{
return _first*(_rest * ...);
}
/** Version 2. */
template <typename ...data_type>
constexpr data_type Product2(data_type ..._rest) // This code does not compile :(
{
return (_rest * ...);
}
将return类型改为auto,数据类型为pack,不能作为return类型使用
template <typename ...data_type>
constexpr auto Product2(const data_type& ..._rest)
{
return (_rest * ...);
}
int main()
{
constexpr auto p = Product2(1, 2, 3);
return 0;
}