当前位置:   article > 正文

c++参数包理解学习_c++ 参数包如何理解

c++ 参数包如何理解

源码如下:

#include <bits/stdc++.h>

template <typename ... Tail> class Tuple;

template<> class Tuple<> {};

template <typename Value, typename ... Tail>
class Tuple<Value, Tail ...> : Tuple<Tail ...> {
    Value Val;
public:
    Tuple() {}
    Tuple(Value value, Tail ... tail) : Val(value), Tuple<Tail ...>(tail ...) {}
    Value value() { return Val; }
    Tuple<Tail ...> next() { return *this; }
};

int main() {
    Tuple<char, double, std::string> tuple('1', 1.5, "Hello World");
    std::cout << tuple.value() << std::endl;
    std::cout << tuple.next().value() << std::endl;
    std::cout << tuple.next().next().value() << std::endl;
    return 0;
}
————————————————
版权声明:本文为CSDN博主「LucienShui」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xs18952904/article/details/85221921
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

Tuple<Tail …> next() { return *this; }的理解如下:
class Tuple<Value, Tail …> : Tuple<Tail …> 是中继承关系。
return *this 只是一种类型的强制转换。
强制转换 成功后,返回基类的value

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/576926
推荐阅读
相关标签
  

闽ICP备14008679号