Calling the base class template constructor in C ++

I have a base class of a template that has a constructor for converting from any other instance of an instance of this class, for example:

template <class T>
class FooBase
{
public:

    FooBase()
    {
    }

    template <class U>
    FooBase(const FooBase<U>& other)
    {
        std::cout << "FooBase<U>" << std::endl;
    }
};

Note that the copy constructor is not defined.

Then I have a derived template class that has a copy constructor, as well as a constructor used to convert:

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)
    {
    }
};

Since it FooBasedoes not have a copy constructor, this leads to a FooBase<T>(other)call to the copy instance created by the compiler. This means that if I run this:

int main()
{
    Foo<int> a;
    Foo<int> b(a);

    return 0;
}

Exit is nothing when it should be 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

, .

?

+6
1

, , , :

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) {}
};
+4

Source: https://habr.com/ru/post/1016242/


All Articles