Mixing variadic template values and variadic deduced types

心不动则不痛 提交于 2019-12-23 07:37: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.


回答1:


From ISO C++ standard's current working draft 14.1 (11):

A template parameter pack of a function template shall not be followed by another template >parameter unless that template parameter can be deduced from the parameter-type-list of >the function template or has a default argument

In your case 'Types' is a function parameter pack and 'Values', that is a template parameter pack, can be always followed by a function parameter pack. Also this code works for the same reason:

#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;
}


来源:https://stackoverflow.com/questions/21101391/mixing-variadic-template-values-and-variadic-deduced-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!