Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-bakin/758a063441c21eb61f48287db3dbac22 to your computer and use it in GitHub Desktop.
Save david-bakin/758a063441c21eb61f48287db3dbac22 to your computer and use it in GitHub Desktop.
Odd example of a template inheriting from its own specialization
#include <iostream>
#include <string>
// Template which inherits from its own specialization, as I first saw in
// https://stackoverflow.com/a/28213747/751579, which surprised me
template <typename T>
struct Foo : public Foo<decltype(T::x)> {
T xxx;
};
template <typename TT>
struct Foo<TT*> {
TT* p;
};
template <typename TT>
struct Foo<TT&> {
TT r;
};
struct Has_x {
std::string* x;
};
int main()
{
std::string s{"I'm 's'"};
Foo<Has_x> foo_has_x;
foo_has_x.p = &s;
std::cout << typeid(foo_has_x).name() << " - " << *foo_has_x.p << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment