Consider the following header file:
// Foo.h
class Foo {
public: template <typename T> void read(T& value);
};
It seems that assigning a pointer to Foo::read<T>
in the constructor of a class, of which variable is then declared, cause instantiation:
// Foo.cc
#include "Foo.h"
template <typename T>
void Foo::read(T& value) { /* do something */ }
template <typename T> struct Bar {
Bar<T>() { void (Foo::*funPtr)(T&) = &Foo::read<T>; }
};
static Bar<int > bar1;
static Bar<long > bar2;
static Bar<float> bar3;
Is this solution reliable / portable / standard-conformant? (It works at least with Intel and GNU compilers.)
If you wonder why not to simply use template Foo::read<int>(int&);
see this question.