c++ - Get template parameter from file -
i have 2 classes:
- a non-type templated class foo
- a non-templated class bar contains member of foo, template parameter should read file.
from (i think) know, doesn't work because template parameters of non-templated class set @ compile-time, need static consts. don't see how can achieve that, since evaluation (i.e. reading file, extracting value) has happen before can provide parameter value. nonetheless, there way achieve similar?
i want avoid templating second class since user has (in theory) no knowledge of file, should looked when program executed. file not change during run-time.
the situation this:
// templated class template <int a> class foo { public: foo() : a_(a) {}; void print () { std::cout << "the value of : " << a_ << std::endl; } private: int a_; }; // non-templated class // won't work, showing idea: class charlie { public: charlie() { = loadfromfile("myfile"); } void print () { std::cout << "called c: " << std::endl; c_.print(); } private: int a; foo<a> c_; };
thanks!
you cannot have template parameters read file, or otherwise determined @ runtime.
template paramters, inherently need deduced/calculated @ compile time. no other way, sorry!
the idea of template parameters can optimized @ compile time, resolving them can't defered runtime.
Comments
Post a Comment