Created
January 12, 2021 17:05
-
-
Save david-bakin/758a063441c21eb61f48287db3dbac22 to your computer and use it in GitHub Desktop.
Odd example of a template inheriting from its own specialization
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 <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