Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created December 4, 2014 10:30
Show Gist options
  • Save jefftrull/561b4e51d03bb3df3f86 to your computer and use it in GitHub Desktop.
Save jefftrull/561b4e51d03bb3df3f86 to your computer and use it in GitHub Desktop.
attempt to partially specialize class by defining a member function template to be different for a certain *class* template parameters
#include <typeinfo>
#include <string>
#include <iostream>
template<typename T> struct A {
template<typename U>
A(U u) {
std::cout << std::string("class template parameter generic, ctor template parameter ") + typeid(u).name() << std::endl;
}
};
struct B {};
// Now a special version of the constructor for class pointer arguments only
template<typename T>
template <typename U>
A<T*>::A(U u) {
std::cout << std::string("class template parameter pointer, function template parameter ") + typeid(u).name() << std::endl;
}
struct X {};
struct Y {};
int main() {
X x;
Y y;
// these should call the default ctor
A<B> abx(x);
A<B> aby(y);
// these should call the (class) specialized version
A<B*> abpx(x);
A<B*> abpy(y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment