Details of c++11--template improvements

Source: Internet
Author: User

C++11 improved the compiler's parsing rules, as much as possible to resolve multiple right angle brackets (>) to the template parameter terminator, easy to write template-related code.

1. Right angle brackets for the template

In the previous C + + standard, the template set template in the right angle bracket can not be linked to a piece, or will be confused with the right shift operator, such as vector< map< int, int> >//Right two > to separate.
In c++11, this restriction is canceled, and the compiler can determine whether the ">>" is the right-shift operator or the end tag of the template parameter.

2. Aliases for templates

Previous C + + uses typedef to assign aliases to types, and in c++11, you can use using to specify aliases.

    typedef std::map< std::string, int> map_int_t;    Using map_int_t = std::map<std::string, int>;    typedef std::map< std::string, std::string> map_str_t;    Using map_str_t = std::map< std::string, std::string>;    If you need to specify a key for map std::string, and value is arbitrary, you have to do this for the previous C + +:    template<typename t>    struct str_map{    typedef std::map< std::string, t> type;    };    //....    Str_map<int>::type Impl;    In c++11, use using to simplify    template<typename t> using    str_map_t = std::map< std::string, t>;// Specifies the type alias ...    .    Str_map_t<int> map_int_t; Defining variables with type aliases

    Use typedef    typedef void (*func_t) (int, int);    Use using using    func_t = void (*) (int, int);    The function pointer to the template parameter    uses typedef    template<typename t>    struct func_t{    typedef void (*type) (T, t);    };    Func_t<int>::type xx_1;    Use    template<typename t> using    func_t = void (*) (T, T);    func_t xx_2; declaring variables

3. Default parameters for function templates

In c++98/03, the class template can have default parameters, as follows:

    Template<typename T, typename u = int, u N = 0>    struct foo{    ...    };

However, default template parameters for functions are not supported

    template< TypeName T = int>//is not supported in c++98/03    void func (void) {    ...    };

In c++11, you can support default parameters for function templates

template< TypeName T = int>//is not supported in c++98/03 void func (void) {...}; int main () {    func ();//used the default template parameter int    return 0;}

when all template parameters have default parameters, the call to the function template is like a normal function. For a class template, even if all parameters have default parameters, they must be instantiated with <> followed by the template name when used.
The default parameters of a function template differ from the other default parameters on the usage rules, for example, there is no need to write to the last position of the parameter table. At the same time, there are no default values or types of parameters that can be deduced automatically

Template<typename r = int, typename u>//Default template parameter does not have to be written in the final position of the parameter table R func (U val) {    return val;} int main (void) {    func (123);  The parameter u is deduced as int return 0 using automatic derivation    ;}

When the function template is called, if the parameters of the specified template are displayed, the parameter fills the order from right to left!!

Func<long> (123); parameter is filled from right to left, then U is considered a long type, then the returned 123 is a long type



automatic derivation of function template parameter types

This automatic derivation of template parameter values is implemented in the C + + language. Any template parameter "value" that can be pushed out is not required to be indicated in the template argument list.
In addition, when the default template parameter and template parameter auto-derivation are used simultaneously, if the function template cannot deduce the parameter type automatically, the compiler will use the default template parameter, otherwise the automatically deduced parameter type will be used. that is, the automatic derivation type takes precedence.

Template<typename t>void F (T val) {    cout << val << Endl;} Tempalte<typename t>struct identity{    typedef T type;}; Template<typename t = int>void func (TypeName Identity<t>::type val, T = 0) {..}; int main () {    f ("Hello World");//template parameters are automatically deduced, T is const char*    func (123);        T is int    func (12,12.0);    T is double because the second parameter in Func is 12.0, so that the parameter template T is automatically deduced as a double    return 0;}

Details of c++11--template improvements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.