Patrick Niedzielski
Patrick Niedzielski

Reputation: 1214

Can a virtual member function definition appear outside the class template?

In a project I'm writing, I have class template that I'm using as a base class, and it has a virtual method that derived classes override. The virtual function also has its own implementation. The problem I'm having, though, can be boiled down to the following code:

#include <iostream>

template <typename T> struct A {
    virtual void do_something()
#ifdef INLINE_CLASS
    { std::cout << "Saluton, mondo!\n"; }
#else
    ;
#endif
};

#ifndef INLINE_CLASS
template <typename T> virtual void A<T>::do_something() {
    std::cout << "Saluton, mondo!\n";
}
#endif

int main(int argc, char** argv) {
    A<int> a;
    a.do_something();
    return 0;
}

When I compile with INLINE_CLASS defined, the code compiles fine, but without it, I get an error with GCC:

pniedzielski@patrick-laptop-debian:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5) 
pniedzielski@patrick-laptop-debian:~$ g++ -std=c++11 -Wall -o test-virtual-template test-virtual-template.cpp 
test-virtual-template.cpp:13:23: error: templates may not be ‘virtual’
pniedzielski@patrick-laptop-debian:~$ g++ -std=c++11 -Wall -DINLINE_CLASS -o test-virtual-template test-virtual-template.cpp 
pniedzielski@patrick-laptop-debian:~$ ./test-virtual-template 
Saluton, mondo!

Usually, in my own code, I would break the implementation out of the class template and put it in a .inl file, but it doesn't seem that I can in this case. Is there something I'm missing? Is this a bug in GCC? Or is the only way to do this according to the standard to put the member function definition in the class template declaration?

Upvotes: 1

Views: 2013

Answers (2)

David G
David G

Reputation: 96790

You don't need virtual when separately implementing the method:

template <typename T>
void A<T>::do_something() {
    std::cout << "Saluton, mondo!\n";
}

Live Demo

Upvotes: 2

Andy Prowl
Andy Prowl

Reputation: 126412

This issue is not related to templates.

You simply shouldn't use the virtual keyword in an out-of-class definition of a member function:

template <typename T> void A<T>::do_something() {
    std::cout << "Saluton, mondo!\n";
}

See this compiling, for instance, in this live example.

Upvotes: 5

Related Questions