Created
September 22, 2017 18:41
-
-
Save ramntry/f56aee1d60a7cd4eff939dd4d13e9bb4 to your computer and use it in GitHub Desktop.
No virtual member function templates in C++03 through C++14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory> | |
struct A { | |
virtual void foo() const { | |
std::cout << "virtual A::foo()'s called" << std::endl; | |
} | |
}; | |
struct B { | |
template <unsigned N> | |
void foo() const { | |
std::cout << "template B::foo<" << N << ">()'s called" << std::endl; | |
} | |
}; | |
template <unsigned M> | |
struct C { | |
void foo() const { | |
std::cout << "template C<" << M << ">::foo()'s called" << std::endl; | |
} | |
}; | |
template <unsigned M> | |
struct D { | |
template <unsigned N> | |
void foo() const { | |
std::cout << "template D<" << M << ">::foo<" << N << ">()'s called" << std::endl; | |
} | |
}; | |
template <unsigned M> | |
struct E { | |
virtual void foo() const { | |
std::cout << "virtual E<" << M << ">::foo()'s called" << std::endl; | |
} | |
}; | |
#ifndef NO_TEMPLATE_VIRTUAL | |
struct F { | |
template <unsigned N> | |
virtual void foo() const { | |
std::cout << "template virtual F::foo<" << N << ">()'s called" << std::endl; | |
} | |
}; | |
#endif | |
int main() { | |
const auto a = std::unique_ptr<A>(new A()); | |
a->foo(); | |
const auto b = std::unique_ptr<B>(new B()); | |
b->foo<1>(); | |
b->foo<2>(); | |
const auto c1 = std::unique_ptr<C<1>>(new C<1>()); | |
const auto c2 = std::unique_ptr<C<2>>(new C<2>()); | |
c1->foo(); | |
c2->foo(); | |
const auto d1 = std::unique_ptr<D<1>>(new D<1>()); | |
const auto d2 = std::unique_ptr<D<2>>(new D<2>()); | |
d1->foo<1>(); | |
d1->foo<2>(); | |
d2->foo<1>(); | |
d2->foo<2>(); | |
const auto e1 = std::unique_ptr<E<1>>(new E<1>()); | |
const auto e2 = std::unique_ptr<E<2>>(new E<2>()); | |
e1->foo(); | |
e2->foo(); | |
#ifndef NO_TEMPLATE_VIRTUAL | |
const auto f = std::unique_ptr<F>(new F()); | |
f->foo<1>(); | |
f->foo<2>(); | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment