[C++11] Effective modern C + + reading notes

Source: Internet
Author: User

This article records some of my own understanding and experience when I read effective modern C + +.

Item1: Template type derivation

1) The reference property cannot pass in a template function by passing a value parameter . This means that if a template function requires a parameter of type reference, it must be declared as reference in the template declaration, otherwise the result of the type deduction will not have the reference attribute, even if the template function is called with a variable of type reference.

2) The constant and volatile properties cannot be passed into the template function through the pass-through parameters , but they can be passed through the reference parameter.

3) If the Universal reference parameter is declared in the template, then the Lvalue argument is deduced as an lvalue reference, and the rvalue argument is deduced as Universal reference (&&).

4) The array type variable and the function type variable will be deduced into the corresponding pointer type variable by the value parameter. If the template is declared as a reference parameter, it is inferred that the array reference (containing the size information of the arrays) and the function reference are deduced.

Item2:auto type derivation

1) The Auto type deduction rule is the same as the template type deduction rule, in addition to handling parameters initialized with {}.

2) The template cannot deduce a parameter that was initialized by {} unless it is declared as a parameter of type std::initializer_list<t>.

#include <iostream>

Template<typename t>

void F1 (T param) {return;}

Template<typename t>

void F2 (std::initializer_list<t> param) {return;}

int main ()

{

Auto i = {1, 2, 3, 4};

F1 (i); Good

F1 ({1, 2, 3, 4}); Error

F2 ({1, 2, 3, 4}); Good

return 0;

}

3) in c++14, a function that returns a value of auto must not return a value initialized with {}. The auto type parameter of the lambda function cannot also be called with the {} initial value as a parameter.

Auto F1 () {return 4;}//good

Auto F2 () {Auto i = {1,2,3,4}; return i; }//good

Error Auto F3 () {return {1,2,3,4};}//error

< to Be continued >

[C++11] Effective modern C + + reading notes

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.