Converting with yaml-cpp to template class

I have my container:

template<class T>
class MyContainer {}

      

And I am using yaml-cpp to load some data into this container. So I need to write a specialization for convert

struct:

template<> struct convert< MyContainer<int> > {};
template<> struct convert< MyContainer<double> > {};
template<> struct convert< MyContainer<char> > {};
template<> struct convert< MyContainer<MyClass> > {};

      

... etc.

In the end, I write:

// ...
node.as< MyContainer<int> >
// ...

      

But the fact is that every specialization is the MyContainer

same for . Therefore, any specialization for is the convert

same and redundant:

template<> struct convert< MyContainer<int> > { /* the same code */ };
template<> struct convert< MyContainer<double> > { /* the same code */ };
template<> struct convert< MyContainer<char> > { /* the same code */ };
template<> struct convert< MyContainer<MyClass> > { /* the same code */ };

      

Is it possible to avoid this garbage by using C ++ itself or some other yaml-cpp functions?

+3


source to share


1 answer


In comments

in fact, the situation is a little more complicated. What confused me is that MyContainer has two template arguments and the converter only has one. So I had to write:template<class A, class B> struct convert< Manager<A, B> > { /**/ };



Try an alternative partial specialization

template <typename... Ts> 
struct convert< MyContainer<Ts...> > { 

    using container_type = MyContainer<Ts...>;

    // ... the specialized implementation, once

};

      

+1


source







All Articles