Skip to content

Instantly share code, notes, and snippets.

@PIlin
Created September 1, 2015 20:48
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 PIlin/7b0d22f72c60b78e874e to your computer and use it in GitHub Desktop.
Save PIlin/7b0d22f72c60b78e874e to your computer and use it in GitHub Desktop.
crtp.cpp
#include <iostream>
using namespace std;
template <typename T, typename U>
struct Base
{
void TestBase(void* ptr) //__attribute__ ((noinline))
{
U& ref = *reinterpret_cast<U*>(ptr);
static_cast<T*>(this)->Test(ref);
}
};
struct Child : public Base<Child, float>
{
void Test(float& val)
{
//cout << __FUNCTION__ << " = " << val << endl;
printf("%f", val);
}
};
int main() {
Child c;
float x = 42.0f;
c.TestBase(&x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment