12

Is the following perfectly defined by the standard ?

#include <iostream>

template <unsigned int... Values, class... Types>
void f(Types&&... values)
{
    std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl;
}

int main()
{
    f<7, 5>(3);
    return 0;
}

It compiles well under g++ 4.8 but I wonder if it is normal.

4

1 回答 1

2

来自ISO C++ 标准的当前工作草案14.1 (11):

函数模板的模板形参包后面不应有另一个模板>参数,除非该模板形参可以从>函数模板的形参类型列表中推导出来或具有默认参数

在您的情况下,“类型”是一个函数参数包,“值”是一个模板参数包,后面总是可以跟一个函数参数包。此代码也出于相同的原因工作:

#include <iostream>

template <class... Values, class... Types>
void f(Types&&... values)
{
    std::cout<<sizeof...(Values)<<" "<<sizeof...(Types)<<std::endl;
}

int main()
{
    f<int, float>(-3, 5);
    return 0;
}
于 2014-03-02T09:44:22.377 回答