如何在 C++11 的构造函数初始化列表中使用一个公共参数初始化元组的元素?

How to initialize elements of a tuple with one common parameter in a constructor initializer list in c++11?

我实际上是在尝试在构造时提供对元组元素的引用。

this 的回答中,构造函数中有一个参数包扩展,为每个元组元素提供相同的参数。我尝试以类似的方式进行操作,但得到的错误与尝试在默认构造函数本身中构造元组时遇到的错误相同,即:

no matching function call to 'std::tuple<T1,T2, ..>::tuple(Base&, Base&,..)'

class Base
{
public:
    Base() :
        Base(*this, *this, *this, *this, // delegating to templated constructor
               *this, *this, *this) 
    {}

    template<typename... T>
    Base(T&&... args) :
        tuple(args...) // error
    {}

private:
    std::tuple<T1, T2, etc..>; // size of 7
};

使用std::make_tuple(使用reference_wrappers)提供参数也会出错,我认为它需要c++14。我正在使用 minGW 4.9.2.

编辑: 元素类型将 Base& 作为其构造函数的参数,如下所示 T(Base&).

class Base
{
// ... 
private:
   std::tuple<T1, T2, T3.. etc> tuple; // all types have this constructor:
                                       // T(Base&);
};

编辑: 应该检查所有错误消息 ;)

C:\..\mingw492_32\i686-w64-mingw32\include\c++\tuple:418: error: no type named 'type' in 'struct std::enable_if<false, void>'
       template<typename... _UElements, typename = typename
                                    ^

这提醒我,也许我的其中一种类型 T(Base&) 声明错误。 B.

问题已解决。即使是构造函数初始化列表中的微不足道的构造也能像您期望的那样工作:

Base() :
    tuple{*this, *this, *this, *this,
          *this, *this, *this}
{}

其中一个类型的构造函数声明错误,我一开始并没有从这两个错误消息中推断出这一点。