37

What is the meaning of tokenizer<> tok(s) line in code below? I know that <> is used while working with templates, but according to my understanding <> should not be empty- it should contain type definition.

    using namespace std;
    using namespace boost;
    string s = "This is, a te\x1Dst";
    cout<<s<<endl;
    tokenizer<> tok(s);
    for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
        cout << *beg << "\n";
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
vico
  • 17,051
  • 45
  • 159
  • 315

3 Answers3

32

It just means the template should use the default parameter(s). For example:

template <int N = 10>
class X { };

X<> x;  // this is an X<10> 

Clearly, it's only possible when all the template parameters have defaults (or for variadic templates that have no mandatory parameters - see below)....

For boost::tokenizer specifically, the template's:

template <
    class TokenizerFunc = char_delimiters_separator<char>, 
    class Iterator = std::string::const_iterator,
    class Type = std::string
>
class tokenizer;

It's not relevant to the "meaning of tokenizer<> tok" that the body of your question focuses on, but to address the more general question title "What is the meaning of empty “<>” in template usage?"...

As Shafik first mentioned, the my_template<> form can also be used with variadic templates such as std::tuple to specify an empty parameter pack:

// namespace std { template <class ...Args> class tuple; }
std::tuple<> t;  // this tuple can't store anything :-.
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
11

The template-argument list can be empty when you have default arguments or you have an argument pack, this is covered in the draft C++ standard section 14.3 Template arguments which says (emphasis mine):

When template argument packs or default template-arguments are used, a template-argument list can be empty. In that case the empty <> brackets shall still be used as the template-argument-list. [ Example:

template<class T = char> class String;
String<>* p; // OK: String<char>
String* q; // syntax error
template<class ... Elements> class Tuple;
Tuple<>* t; // OK: Elements is empty
Tuple* u; // syntax error

—end example ]

In your case boost::tokenizer has the following default arguments:

template <
    class TokenizerFunc = char_delimiters_separator<char>, 
    class Iterator = std::string::const_iterator,
    class Type = std::string >

which allows your specific case to work.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
6

It means that default template arguments are used.

For example, if you had a template :

template < typename T = int >
struct A;

then this would have type int for the template argument :

A<> a;
BЈовић
  • 62,405
  • 41
  • 173
  • 273