Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of empty "<>" in template usage?

Tags:

c++

templates

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"; } 
like image 799
vico Avatar asked Sep 12 '14 09:09

vico


People also ask

What does template <> mean in C++?

A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.

What are non-type parameters for templates?

A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument. A non-type parameter can be any of the following types: An integral type. An enumeration type.

Why do we use template template parameter?

Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.


1 Answers

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 :-. 
like image 97
Tony Delroy Avatar answered Oct 13 '22 01:10

Tony Delroy