I have a problem in wrapping a template class in a non-template one;
I'm trying to make this code work:
my_class.hpp
#ifndef MY_CLASS_HPP
#define MY_CLASS_HPP
#include <vector>
#include <iostream>
class VectorPrinter {
private:
template<class value_type>
static std::vector<value_type> vec;
public:
template<class value_type>
VectorPrinter(const std::vector<value_type>& vec_)
{
vec = vec_
for (std::size_t i = 0; i<vec.size(); ++i)
std::cout << vec[i] << std::endl;
}
};
#endif /* MY_CLASS_HPP */
main.cpp
#include "my_class.hpp"
#include <string>
int main() {
std::vector<int> v1{1, 2, 3, 4, 5};
std::vector<std::string> v2{"Hello", "world", "!"};
std::vector<double> v3{2.5, 7.3, -5.1};
VectorPrinter vp1(v1);
VectorPrinter vp2(v2);
VectorPrinter vp3(v3);
return 0;
}
I get the following error message while trying to compile (I tried -std=c++11, -std=c++14, -std=c++17: no difference; no difference neither switching from g++ to clang):
./my_class.hpp:19:5: error: cannot refer to variable template 'vec' without a template argument list
Now: my goal is to circumvent defining VectorPrinter as a template class or, alternatively, avoid specifying the template argument in the case VectorPrinter cannot be non-template; so my problem reside within the scope of either variable template or template argument deduction.
This is just a test for my thesis project; what I need to achieve, at the end, is being able to define a template library for RNG and to encapsulate these classes in more complex ones which perform Direct Simulation Monte Carlo. So, at the end I would like something like:
template <class engineType>
class RngObject {
// ...
};
class Ensemble {
private:
template<class engineType> RngObject<engineType> rng;
// ...
};
It would be quite boring having to define each and every class that encapsulate a RNG as template; moreover, I am asked to avoid using dynamic time polymorphism, at leat at this stage.
I hope someone can provide me some useful suggestion. Thanks