开发者

Specialize template for any vector<any_arithmetic_data_type>

开发者 https://www.devze.com 2023-04-09 10:49 出处:网络
I have got a template method with two specialized versions for type bool and vector<string>. Base version:

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
{
  ...
}  

te开发者_如何学Gomplate <>
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
{
}


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.


OK uber complicated, but I got it all working. I couldn't just check is_integral on the value_type type inside the first (default) function overload as this would cause SFINAE to remove the overload for non-vectors.

Unlike Nawaz's solution, this doesn't require a dummy parameter to be added, but it does require an enable_if condition on the default function template.

This works on VS2010.

#include <vector>
#include <string>
#include <type_traits>

using namespace std;

template <typename T> struct is_vector { static const bool value = false; };
template <typename T> struct is_vector< std::vector<T> > { static const bool value = true; };

// metafunction to extract what type a vector is specialised on
// vector_type<vector<T>>::type == T
template <class T>
struct vector_type
{
private:
    template <class T>
    struct ident
    {
        typedef T type;
    };

    template <class C> 
    static ident<C> test(vector<C>);

    static ident<void> test(...);

    typedef decltype(test(T())) vec_type;
public:
    typedef typename vec_type::type type;
};

// default version
template <class T>
const typename enable_if<!is_vector<T>::value || !is_integral<typename vector_type<T>::type>::value, T>::type
InternGetValue(const std::string& pfad)
{
    return T();
}

// bool specialisation
template <>
const bool
InternGetValue<bool>(const std::string& pfad)
{
    return true;
}

// vector<string> specialisation
template <>
const vector<string>
InternGetValue<vector<string>>(const std::string& pfad)
{
    return vector<string>();
}

// vector<T> specialisation (where T is integral)
template <class T>
const typename enable_if<is_vector<T>::value && is_integral<typename vector_type<T>::type>::value, T>::type
InternGetValue(const std::string& pfad)
{
    return T();
}

int main()
{
    string x;
    auto a = InternGetValue<int>(x);
    auto b = InternGetValue<bool>(x);
    auto c = InternGetValue<vector<string>>(x);
    auto d = InternGetValue<vector<pair<int, int>>>(x);
    auto e = InternGetValue<vector<int>>(x);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消