Here is a simple piece of code :
#include <iostream>
#include <fstream>
template<typename T>
class A {
public:
T _v;
template<unsigned short V> void init() { _v *= V; }
void print(double txt) { std::cout << "Initialized with ?? : " << txt << std::endl; }
};
int main() {
A<double> foo;
foo._v = 3.5;
foo.init<2>();
foo.print(foo._v);
A<int> bar;
bar._v = 2;
bar.init<5>();
foo.print(bar._v);
}
I would like to have an implementation of the function A::print(double) dependent on the unsigned short V, for example, replace ?? by the unsigned short from which init() has been instanciated. My questions are: (a) is it doable ? (b) if (a), how ?
While searching if (a), I thought I could add a functor to the class A<T>, initialize its state (with the value of V) in init(), call it in print(double), but I have never used such objects so I have no idea if this is the way to go. I’m basically open to any suggestions, the only thing I need is that the call to print remains the same (because I’ll call it from other classes who have no idea of the value of unsigned short V.
Thanks !
One way:
Or why not: