Sasha
Sasha

Reputation: 109

Virtual inheritance, explicit instantiation—returning derived class reference / pointer (covariant types)

.hpp

template <typename T>
struct A { virtual A& modify() = 0; };

template <typename T>
struct B : virtual A<T> {};

template <typename T>
struct C : B<T> { C& modify() final; };

.cpp

template <typename T>
C<T>& C<T>::modify() {
    // …
    return *this;
}

// explicit instantiation
template struct C<double>;

I need some methods to return references in order to make “chains” / define an assignment operator / etc.:

C<double> a, b, c;
// …    
a = (b = c).modify();

I also have to deal with virtual inheritance to avoid “the diamond problem” (omitted here for simplicity).

However, this does not work:

MSVC:

Error C2908: explicit specialization; 
'A<T> &C<T>::modify(void)' has already been instantiated

Explicit instantiation works fine w/o virtual inheritance. So I am wondering what is wrong here. (Also everything works fine if there are no member functions returning object references / pointers.)

Upvotes: 0

Views: 197

Answers (1)

marcinj
marcinj

Reputation: 49976

The correct syntax for explicit instantiation should be:

template struct C<double>;
         ^^^^^

also, you still need to specify type parameter for your C template:

C<double> a, b, c;
  ^^^^^^

at least g++ and clang accepts this code: http://coliru.stacked-crooked.com/a/23ba6a238a7a17da

but Visual Studio does not...


looks like VS does not like covariant return types, the following compiles under g++/clang and VS but - no covariant return in modified() : http://coliru.stacked-crooked.com/a/70c8e64f0824129a

Upvotes: 1

Related Questions