Understanding how template default parameter values ​​are declared

In the std :: basic_string documentation found at http://en.cppreference.com/w/cpp/string/basic_string , the basic_string class is declared as follows.

template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_string; 

However, in GCC and Visual Studio, the default values ​​for the template and Allocator template parameters are not specified in the class declaration.

Below from basic_string.h from GCC 4.9.2.

 template< typename _CharT, typename _Traits, typename _Alloc > class basic_string 

Note that there are no default values ​​for the _Traits and _Alloc template parameters.

What am I missing?

+5
source share
2 answers

These classes are usually declared in millions of places 1 . Only one of these declarations will contain default arguments, because otherwise it is a mistake.

For basic_string , in libstdc ++, the default arguments are in the forward declaration in bits/stringfwd.h (which is included, among other things, in <string> ):

  template<typename _CharT, typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT> > class basic_string; 

1 Should not be taken literally.

+10
source

in the visual studio defined in <xstring> at the beginning before specialization:

 template<class _Elem, class _Traits = char_traits<_Elem>, class _Ax = allocator<_Elem> > class basic_string; 

The "regular" string types are defined later in the typedef for std::string :

 typedef basic_string<char, char_traits<char>, allocator<char> > string; typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring; 
+2
source

All Articles