了解如何声明模板参数的默认值

Ben*_*Key 5 c++ templates

http://en.cppreference.com/w/cpp/string/basic_string中的std :: basic_string文档中,basic_string类声明如下.

template<
    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>
> class basic_string;
Run Code Online (Sandbox Code Playgroud)

但是,在GCC和Visual Studio中,Traits和Allocator模板参数的默认值未在类声明中指定.

以下内容来自GCC 4.9.2的basic_string.h.

template<
    typename _CharT,
    typename _Traits,
    typename _Alloc
> class basic_string
Run Code Online (Sandbox Code Playgroud)

请注意_Traits和_Alloc模板参数缺少默认值.

我错过了什么?

T.C*_*.C. 10

这些类通常在一百万个地方宣布1.这些声明中只有一个会带有默认参数,否则会出错.

因为basic_string,在libstdc ++中,默认参数可以在前向声明中找到bits/stringfwd.h(<string>除其他外包括):

  template<typename _CharT, typename _Traits = char_traits<_CharT>,
           typename _Alloc = allocator<_CharT> >
    class basic_string;
Run Code Online (Sandbox Code Playgroud)

1不要按字面意思理解.