简体   繁体   English

如何初始化不可默认构造的不可复制对象的元组?

[英]How to initialize a tuple of non-default-constructible not-copyable objects?

Given some classes with parameterized constructors, such as: 给定一些带有参数化构造函数的类,例如:

class A
{
public:
    A(bool b, int i) { /*...*/ }
private:
    A(const A&) {}
};
class B
{
public:
    B(char c, double d) { /* ... */ }
private:
    B(const B&) {}
};

How to properly initialize a tuple of such classes? 如何正确初始化此类的元组?

boost::tuple<A,B> tup( /* ??? */ );

Not using copy constructor of A or B, and, if possible, not using move-constructor either. 不使用A或B的副本构造函数,并且如果可能,也不使用move-constructor。 C++03 solution preferred, if possible. 如果可能,首选C ++ 03解决方案。

Can you just add a piecewise constructor for your types? 您可以为您的类型添加分段构造器吗? If so, you can create a horrible macro that unpacks and delegates a tuple: 如果是这样,您可以创建一个可怕的宏来解包并委托一个元组:

#define CONSTRUCT_FROM_TUPLE(CLS)                      \
    template <class... Ts>                             \
    CLS(std::tuple<Ts...> const& tup)                  \
        : CLS(tup, std::index_sequence_for<Ts...>{})   \
    { }                                                \
                                                       \
    template <class Tuple, size_t... Is>               \
    CLS(Tuple const& tup, std::index_sequence<Is...> ) \
        : CLS(std::get<Is>(tup)...)                    \
    { }

And just add it to your types: 并将其添加到您的类型中:

struct A {
    A(bool, int ) { }
    A(const A& ) = delete;
    CONSTRUCT_FROM_TUPLE(A)
};

struct B {
    B(char, double ) { }
    B(const B& ) = delete;
    CONSTRUCT_FROM_TUPLE(B)
};

And pass in tuples: 并传入元组:

std::tuple<A, B> tup(
    std::forward_as_tuple(true, 42), 
    std::forward_as_tuple('x', 3.14));

Pre-C++11, I don't know that this is possible - you don't have delegating constructors at all. 在C ++ 11之前的版本中,我不知道这是可能的-您根本没有委派构造函数。 You'd have to either: 您必须:

  1. Write your own tuple -like class that accepts tuples in its constructor 编写自己的tuple的类,在其构造函数中接受元组
  2. Add tuple constructors to your types that explicitly initialize the same thing as the non-tuple versions did 在您的类型中添加元组构造函数,以显式初始化与非元组版本相同的内容
  3. Have a tuple of types that are single-argument constructible, like boost::tuple<boost::scoped_ptr<A>, boost::scoped_ptr<B>>(new A(...), new B(...)) 具有可单参数构造的元组类型,例如boost::tuple<boost::scoped_ptr<A>, boost::scoped_ptr<B>>(new A(...), new B(...))

(1) is a lot of work, (2) is code duplication and error prone, and (3) involves now having to do allocation all of a sudden. (1)的工作量很大,(2)代码重复且容易出错,(3)现在涉及突然进行分配。

您可以使用以下内容:

tuple<A,B> tup(A(true, 42), B('*', 4.2));

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

相关问题 如何初始化std :: array <T, 2> 其中T是不可复制的,非默认可构造的? - How to initialize an std::array<T, 2> where T is non-copyable and non-default-constructible? 如何正确初始化非默认可构造的类成员? - How to properly initialize non-default-constructible class member? std::map emplace 不可移动不可复制非默认可构造类型 - std::map emplace non-movable non-copyable non-default-constructible type 如何创建C ++ 11不可默认构造的分配器? - How to create a C++ 11 non-default-constructible allocator? 移动具有非默认构造 class 保护的构造函数 - Move constructor with protection for non-default-constructible class 如何使用 C++ 11 样式的非默认构造分配器指定初始大小? - How to specify an initial size using C++ 11 style non-default-constructible allocator? 初始化非默认可构造元素的 std::array? - Initializing an std::array of non-default-constructible elements? 使用 Boost.Python 导出非默认可构造类 - Exporting non-default-constructible classes with Boost.Python 创建非默认可构造类的虚拟对象 - Create dummy object of non-default-constructible class 使用非默认可构造类型填充std :: array(无可变参数模板) - Populate std::array with non-default-constructible type (no variadic templates)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM