DuffyChan DuffyChan - 3 months ago 7
C++ Question

Template parent class with no type parameter

On boost documentation, there is:

class times_two_visitor
: public boost::static_visitor<>
{
public:
void operator()(int & i) const
{
i *= 2;
}
void operator()(std::string & str) const
{
str += str;
}

};


What is the meaning of boost::static_visitor<> in the class declaration?
It looks a like a template specialization but without any specific types. So I am very confused.

Answer

You can use a template, class template as well as function template, using <> for template parameters if the template has default values of all the template parameters.

E.g.

template <typename T = int> struct Foo {};

Foo<double> f1;  // Explicitly specified template paramter
Foo<> f2;        // Default template parameter, int, is used.

template <typename T1 = int, typename T2 = double> struct Bar {};

Bar<float> b1;  // T1 = float, T2 = double
Bar<> b2;       // T1 = int, T2 = double

PS

I am surprised at the use of boost::static_visitor<> since that class template does not seem to have a default value of the template parameter.

Comments