简体   繁体   English

具有可变参数模板的好奇重复模板模式(C ++)

[英]Curiously recurring template pattern with variadic templates (C++)

I have the following piece of code: 我有以下代码:

#include <iostream>

template <typename Derived>
struct Base {
    void print() const { static_cast<const Derived*>(this)->print(); }  
};

struct Value : public Base<Value> {
    int i;
    void print() const { std::cout << "Value: " << i << std::endl; }
    Value(int j) : i(j) {}
};

void do_variadic_thing() {}

template <typename Derived, typename... Args>
void do_variadic_thing(const Base<Derived>& b, Args... args) {
    std::cout << "Inside do_variadic_thing" << std::endl;
    b.print();
    do_variadic_thing(args...);
}

template <typename Derived>
void do_thing(const Base<Derived>& b) {
    std::cout << "Inside do_thing" << std::endl;
    b.print();
    do_variadic_thing(b, b, b);
}

int main(int argc, char** argv) {
    do_thing(Value(1));
}

This code uses the Curiously Recurring Template Pattern define a compile-time polymorphic class with a method called print . 该代码使用“好奇地重复使用的模板模式”,通过一个名为print的方法定义了一个编译时多态类。

What I want to do is to run the print method from a function with a variable number of arguments ( do_variadic_thing ). 我想做的是从带有可变数量参数( do_variadic_thing )的函数中运行print方法。 The code provided above compiles, but produces a strange output: 上面提供的代码可以编译,但是会产生奇怪的输出:

Inside do_thing
Value: 1
Inside do_variadic_thing
Value: 1
Inside do_variadic_thing
Value: 4206337
Inside do_variadic_thing
Value: 4206337

I do not understand why the value printed changes after the second recursive call within do_variadic_thing . 我不明白为什么在do_variadic_thing第二次递归调用后打印的值会发生变化。 The argument b is replicated 3 times; 参数b被重复3次; its type is also the same (ie Base<Value> ). 其类型也相同(即Base<Value> )。 It seems that somehow, after the second call, the arguments no longer refer to some valid memory. 似乎在第二次调用后,这些参数不再引用某些有效内存。

How is this possible? 这怎么可能?

Pass by reference: 通过参考传递:

void do_variadic_thing(const Base<Derived>& b, const Args&... args)
                                                         ^
                                                         here

您正在按值传递第二个/第三个实例-考虑如何构造副本。

The type of the variable b is Base<Value> const& , but the object it refers to is of type Value . 变量b的类型为Base<Value> const& ,但它引用的对象的类型为Value When you copy b to replicate it inside do_thing , you are slicing it; 当您复制b以在do_thing复制它时,您正在对其进行切片 that is, only copying the Base<Value> part. 也就是说,仅复制Base<Value>部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 奇怪的重复模板模式(CRTP),AutoLists和C ++ - Curiously Recurring Template Pattern (CRTP), AutoLists and C++ 如何在C ++中强制使用奇怪的重复模板模式 - How to force use of curiously recurring template pattern in C++ 奇怪的重复模板模式多态拷贝(C ++)中的继承 - Inheritance in curiously recurring template pattern polymorphic copy (C++) C ++ BigIntegers和奇怪的重复模板模式问题 - C++ BigIntegers and the Curiously Recurring Template Pattern Issue C ++反复出现的模板模式,语法错误 - C++ Curiously recurring template pattern, syntax error 我可以在这里使用 Curiously Recurring Template Pattern (C++) 吗? - Can I use the Curiously Recurring Template Pattern here (C++)? C ++:奇怪的重复模板模式是什么?并且可以奇怪地重复模板模式取代虚拟功能? - C++: what is the Curiously-Recurring-Template-Pattern? and can Curiously-Recurring-Template-Pattern replace virtual functions? 具有ptrhead的奇怪重复模板模式 - Curiously recurring template pattern with ptrhead 带有重复出现的模板模式的数组? - Arrays with Curiously Recurring Template Pattern? 类中的方法是否使用现代 c++ 编译器内联的“奇怪重复的模板模式” - Are methods in classes using the 'Curiously Recurring Template Pattern' inlined by a modern c++ compiler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM