Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement optional template parameters?

How do I implement optional template parameters?

I'd like to have a class MyStruct<T1,T2,T3>, where it's allowed to use only the first or the first two params. Now, the functions that process MyStruct<T1,T2,T3> should also somehow deal correctly with the unused template params.

Example:

#include <iostream>

template<class T1, class T2, class T3>
struct MyStruct {
  T1 t1; T2 t2; T3 t3;
  MyStruct() {}
  MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_)
    : t1(t1_), t2(t2_), t3(t3_) {}
};

template<class T1, class T2, class T3>
MyStruct<T1, T2, T3> myplus(MyStruct<T1, T2, T3> const& x,
                MyStruct<T1, T2, T3> const& y) {
  return MyStruct<T1, T2, T3>(x.t1 + y.t1, x.t2 + y.t2, x.t3 + y.t3);
}

int main() {
  typedef MyStruct<int, double, std::string> Struct;
  Struct x(2, 5.6, "bar");
  Struct y(6, 4.1, "foo");
  Struct result = myplus(x, y);
  // (8, 9.7, "barfoo")
  std::cout << result.t1 << "," << result.t2 << "," << result.t3;
}

Now I'd like to change the code so that the above main() function still works, but the following would also work:

typedef MyStruct<std::string, int> Struct;
// result: ("barfoo", 5)
Struct result = myplus(Struct("bar", 2), Struct("foo", 3));

Or this:

typedef MyStruct<int> Struct;
// result: (5)
Struct result = myplus(Struct(2), Struct(3));

I think boost::tuple uses a similar trick, where you may use boost::tuple<A>, boost::tuple<A,B>, boost::tuple<A,B,C>, but I'm not sure how they do it.

like image 570
Frank Avatar asked Jan 26 '26 08:01

Frank


1 Answers

If I get you correctly you should be able to pass default parameters for your template:

template<class T1, class T2 = Default, class T3 = Default>

Where you can substitue any type for the Default.

like image 199
devsnd Avatar answered Jan 28 '26 22:01

devsnd