混合可变参数模板值和可变参数推导的类型 [英] Mixing variadic template values and variadic deduced types

查看:61
本文介绍了混合可变参数模板值和可变参数推导的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是由标准完美定义的吗?

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

g ++ 4.8 ,但我想知道这是否正常。

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

推荐答案

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

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

这篇关于混合可变参数模板值和可变参数推导的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆