c++ - 混合可变参数模板值和可变参数推导类型

标签 c++ c++11 templates variadic-templates template-argument-deduction

标准是否完美定义了以下内容?

#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下编译很好,不知道是否正常。

最佳答案

来自 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

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

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

关于c++ - 混合可变参数模板值和可变参数推导类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21101391/

相关文章:

c++ - 为什么在 C++ 中有不同的变量初始化方式?

c++ - C++ 中的模板 lambda 表达式

c++ - 通过解析Makefile检索使用的头文件和源文件名

c++ - 为什么 STL_tree.h 中的 end() 返回对迭代器对象的引用,而 begin() 返回对象?

c++ - 具有共享资源的 D3DX11SaveTextureToFile

c++ - 循环静态链接库会导致更大的输出大小吗?

c++ - 捕获测试用例顺序

c++ - 多种类型的模板方法特化

c++ - 为模板参数使用类型定义的默认类型

html - 如何在不覆盖现有 CSS 的情况下包含多个 CSS?