具有可变参数模板构造函数的基类不会从派生类复制构造

时间:2014-05-18 01:41:22

标签: c++ templates copy-constructor variadic

当我使用visual studio 2013编译此代码时,我收到错误:

error C2664: 'std::array<int,10>::array(const std::array<int,10> &)' : cannot convert argument 1 from 'initializer-list' to 'const std::array<int,10> &'

我认为任何派生类都可以静态地(切片)到它的基类,为什么这不起作用?

而且我认为即使没有明确的演员也会有效?

#include <array>

template <typename T>
class A {
public:
    std::array<T, 10> data;
    template <typename... Z>
    A(Z... ts) : data({ ts... }) { }
};

template <typename T>
class B : public A<T> {
public:
    template <typename... Z>
    B(Z... ts) : A<T>({ ts... }) { }
};

int main()
{
    B<int> w(1,4,3);
    A<int> g(w); //cant construct from derived class
    A<int> h(static_vast< A<int> >(w)); //not even with cast

    A<int> j(4,5);
    A<int> t(j);  //regular copy constructor works

    B<int> p(t); //works, can construct derived from base
    return 0;
}

0 个答案:

没有答案