C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 表达式包含未展开的参数包大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_489_0@
不知怎的,我不知道如何扩展可变的模板参数包.以下代码有什么问题?
#include <iostream>

template <typename T>
struct print_one
{
    static void run(const T& t)
    {
        std::cout << t << ' ';
    }
};

template<typename... Args>
void print_all(Args&&... args)
{
    // the next line doesn't compile:
    print_one<Args>::run(std::forWARD<Args>(args))...;
}

int main()
{
    print_all(1.23,"foo");
}

Clang说,Expression包含未展开的参数包的Args和args.为什么?

解决方法

…必须在函数调用括号内进行:
print_one<Args>::run(std::forWARD<Args>(args)...);

显然,这不会对你的函数只有一个参数,所以你需要找到一种方法来扩展调用函数调用或其他允许的结构:

// construcTing a dummy array via uniform initialization
// the extra 0 at the start is to make it work when the pack is empty
int dummY[]{0,(print_one<Args>::run(std::forWARD<Args>(args)),0)...};

// or,if your compiler doesn't support uniform initialization
int dummY[] = {0,calling a dummy function
template<typename... Args> void dummy(Args...) {}
dummy((print_one<Args>::run(std::forWARD<Args>(args)),0)...);

// or,construcTing a temporary dummy object
struct dummy { dummy(std::initializer_list<int>) {} };
dummy{(print_one<Args>::run(std::forWARD<Args>(args)),construcTing a temporary initializer list
std::initializer_list<int>{(print_one<Args>::run(std::forWARD<Args>(args)),0)...};

请注意,使用逗号运算符将print_one的void返回值转换为适合放置在参数列表或初始化程序表达式中的值.

初始化器列表表单优先于函数调用表单,因为它们(应该是)排序的LTR函数调用参数不是.

参数包扩展的形式可以由14.5.3 [temp.variaDic]覆盖:

您的原始代码是非法的,因为尽管在文本上可能会出现一个由多个逗号运算符表达式组成的语句,这不是14.5.3:4允许的上下文.

大佬总结

以上是大佬教程为你收集整理的c – 表达式包含未展开的参数包全部内容,希望文章能够帮你解决c – 表达式包含未展开的参数包所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。