Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding code duplication in a specialized template

Tags:

I want to have two similar templates, one with 1 parameter and other with 2 parameters:

template<typename T1, typename T2=void>
struct foo {
  T1 m_t1;
  T2 m_t2;
  foo(T1 t1, T2 t2) : m_t1(t1), m_t2(t2) {}
  T1 t1() { return m_t1; }
  T2 t2() { return m_t2; }
};

template<typename T1>
struct foo<T1,void> {
  T1 m_t1;
  foo(T1 t1) : m_t1(t1) {}
  T1 t1() { return m_t1; }
};

Notice the code duplication for all the T1-related stuff. How can I avoid this?