Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialize template for any vector<any_arithmetic_data_type>

I have got a template method with two specialized versions for type bool and vector<string>.

Base version:

template <class T> 
const T InternGetValue(const std::string& pfad) const
{
  ...   
}

Specialized versions:

template <>
const bool InternGetValue(const std::string& pfad) const
{
  ...
}  

template <>
const std::vector<std::string> InternGetValue< std::vector<std::string>>(const std::string& pfad) const
{
...
}

Now I would like to implement one specialization that will accept all types of vector<aritmethic_data_type> like vector<double> vector<int> or vector<float>.

I could achieve this by writing overloads for the above types, but I'm interested in reaching my goal with another specialization.

This is what I tried so far (leads to error 'illegal use of explicit template arguments'):

template <class T>
const std::vector<T> InternGetValue< std::vector<T>>(const std::string& pfad, typename boost::enable_if<boost::is_arithmetic<T>>::type* dummy = 0) const
{
}
like image 324
nabulke Avatar asked Dec 28 '25 06:12

nabulke


1 Answers

I think std::enable_if and std::is_integral together can solve this problem:

template<typename T>
std::vector<typename std::enable_if<std::is_integral<T>::value, T>::type> 
f(const std::string& d);

If std:: doesn't have them, then use boost:: if you can. It has them.

like image 110
Nawaz Avatar answered Dec 30 '25 22:12

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!