Zee*_*bit 6 c++ inheritance templates constructor
我有一个模板基类,它有一个构造函数,用于从该类的任何其他模板实例化转换,如下所示:
template <class T>
class FooBase
{
public:
    FooBase()
    {
    }
    template <class U>
    FooBase(const FooBase<U>& other)
    {
        std::cout << "FooBase<U>" << std::endl;
    }
};
注意没有定义复制构造函数.
然后我有一个派生模板类,它有一个复制构造函数,以及用于转换的构造函数:
template <class T>
class Foo : public FooBase<T>
{
public:
    Foo()
    {
    }
    Foo(const Foo& other) :
        FooBase<T>(other)
    {
    }
    template <class U>
    Foo(const Foo<U>& other) :
        FooBase<T>(other)
    {
    }
};
因为FooBase没有复制构造函数,所以这会导致FooBase<T>(other)调用编译器生成的复制构造函数.这意味着如果我运行这个:
int main()
{
    Foo<int> a;
    Foo<int> b(a);
    return 0;
}
什么时候输出都没有FooBase<U>.
当然,我可以尝试通过创建一个拷贝构造函数来解决问题FooBase,用委托构造函数:
    FooBase(const FooBase& other)
        : FooBase<T>(other)
    {
    }
但不幸的是,这不起作用,并且它将导致递归调用,因为编译器有帮助地指出:
warning C4717: 'FooBase<int>::FooBase<int>': recursive on all control paths, function will cause runtime stack overflow
所以唯一的解决方案是将逻辑复制到两个构造函数中.
有没有办法绕过这个不涉及代码重复或单独的初始化函数?
您可以拥有第三个私有构造函数,复制构造函数和构造函数模板都委托给它,并且其中包含实际工作:
template <typename T> class FooBase
{
    struct Tag{};
    template <typename U>  // May have U = T
    FooBase(Tag, const FooBase<U> & rhs)
    {
        // actual implementation
    }
public:
    FooBase(const FooBase & rhs) : FooBase(Tag(), rhs) {}
    template <typename U>  // Never selects U = T
    FooBase(const FooBase<U> & rhs) : FooBase(Tag(), rhs) {}
};
| 归档时间: | 
 | 
| 查看次数: | 919 次 | 
| 最近记录: |