我实际上正在尝试在构造时提供对元组元素的引用。
在此答案中,构造函数中有一个参数包扩展,为每个tuple元素提供相同的参数。我曾尝试以类似的方式进行操作,但遇到的错误与尝试在默认构造函数本身中构造元组时遇到的错误相同,即:
没有匹配的函数调用
'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}
{}
其中一种类型的构造函数声明错误,起初我没有从这两条错误消息中推断出这一点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句