简体   繁体   English

将可变参数模板转发给几个类成员

[英]Forward variadic template args to several class members

Is the following safe? 以下是安全的吗? Won't the std::string be move d after the first class member is initialised? 初始化第一个类成员后, std::string不会move吗? It prints out ok but I'm not sure. 打印出来好,但我不确定。

template <typename T>
class Test
{
public:

    template <typename... Args>
    Test(Args&&... args)
    :  m_one(new T(std::forward<Args>(args)...)),
       m_two(new T(std::forward<Args>(args)...))    // <- Here
    {
    }

private:
    std::unique_ptr<T> m_one;
    std::unique_ptr<T> m_two;
};


class C
{ 
public:

    C(int a, int b, const std::string& c)
    :  m_a(a),
       m_b(b),
       m_c(c)
    {
        std::cout << "ctor a=" << m_a << ", b=" << m_b << ", c=" << m_c << "\n";
    }

    int m_a;
    int m_b;
    std::string m_c;
};


int main()
{
     Test<C> t(1, 2, "3");
}

I guess it's ok since the third ctor param of C is const std::string& , but how do I prevent perfect forwarding in a class that takes an r-value ref, eg C(int, int, std::string&&) as then m_two will not receive the same ctor args as m_one ? 我想这没关系,因为C的第三个ctor参数是const std::string& ,但是如何在一个带有r值ref的类中阻止完美转发,例如C(int, int, std::string&&) m_two不会收到与m_one相同的ctor args?

Changing the ctor of Test to 将测试的ctor更改为

   template <typename... Args>
   Test(Args&... args)

doesn't compile. 不编译。 Nor does removing the std::forward<Args>(args)... from m_one and m_two ctors. 也没有从m_onem_two中删除std::forward<Args>(args)...

You are going to want to use something like this: 你会想要使用这样的东西:

#include <memory>
#include <string>
#include <iostream>
#include <utility>

template <typename T>
class Test
{
public:

    template <typename... Args>
    Test(Args&&... args)
    :  m_one(new T(args...)),                    // avoid moving the first time
       m_two(new T(std::forward<Args>(args)...)) // but allowing moving the last time
    {
    }

private:
    std::unique_ptr<T> m_one;
    std::unique_ptr<T> m_two;
};


class C
{
public:

    C(int a, int b, std::string c) // rule of thumb -- if you are going to copy an argument
                                   // anyway, pass it by value.
    :  m_a(a),
       m_b(b),
       m_c(std::move(c)) // you can safely move here since it is the last use. 
    {
        std::cout << "ctor a=" << m_a << ", b=" << m_b << ", c=" << m_c << "\n";
    }

    int m_a;
    int m_b;
    std::string m_c;
};

For m_one , the arguments use lvalue references, so no moving will take place. 对于m_one ,参数使用左值引用,因此不会发生移动。 For m_two , the std::forward will use rvalue references as appropriate. 对于m_twostd::forward将根据需要使用rvalue引用。 Taking the std::string argument to C by value and using std::move makes it work properly for either case. std::string参数取值为C并使用std::move使其适用于任何一种情况。 If you pass an lvalue reference, then the argument will be copy-constructed, but if you pass an rvalue reference, the argument will be move-constructed. 如果传递左值引用,则参数将被复制构造,但是如果传递右值引用,则参数将被移动构造。 In either case, you can move the argument into your m_c member for efficiency. 在任何一种情况下,您都可以将参数移动到m_c成员中以提高效率。

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

相关问题 非模板类的可变参数成员 - Variadic members in non-template class 制作可变参数模板类的forward_list - Making a forward_list of a variadic template class 初始化和访问可变参数类模板的成员 - Initialzing and accessing members of a variadic class template 使用外部类的可变参数模板中的args部分特化可变参数模板内部类是否合法 - Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class 存储可变参数模板args并在另一个类中使用它们 - Store variadic template args and use them in another class 将参数包args解压缩到可变参数模板中定义的每个类的构造函数中 - Unpacking parameter pack of args into the constructor of each class defined in a variadic template 如何处理可变参数模板函数作为 class 构造函数和 class 成员 - how to handle variadic template functions as class constructor and class members 如何给类的可变参数模板模板参数 - How to give several variadic template template arguments to class 如何在可变参数/模板类构造函数中正确使用std :: forward - How to properly use std::forward in variadic/template class constructor 在可变参数模板容器类中给定可变参数args的进出方法 - Call in and out methods given variadic args from a variadic template container class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM