C + + template argument inference

Source: Internet
Author: User

1 type conversions and template arguments

1) Conversions only: const conversion, array and function-to-pointer conversions

Note: Different sizes of the same element types are different types

2) The same argument type must be the same for the same template parameter name

3) The argument types corresponding to different template parameter names can be different, but must be compatible


2 return value issues for function templates

The function template infers only the type of the function argument list and does not infer the return value

Workaround:

1) Display template parameters

Note: The display specifies that the template type parameter is the same as the normal function parameter on the type conversion

Template <typename T1, TypeName T2, TypeName t3>

T1 alternative_sum (T2, T3)

------------------------------------------------------------------------------

Auto V = alternative_sum<int> (i, j);

V is int

2) Tail return type

Template <typename it>

Auto FCN (it first, it larst), Decltype (*first)

{

return *first;

}

Note: The decltype operator of an iterator can only return a reference to an object, not a value.

Can be Remove_reference<decltype (*first) >::type

3 function pointers and argument inference

The program context must satisfy: the type or value can be uniquely determined for each template parameter


4 References and template argument inference

1) lvalue Reference

t&---> Arguments must be an lvalue

F1 (i) I (int) T int

F1 (CI) CI (const int) T const INT

F1 (5) error, not lvalue

Const t&---> Arguments can be objects (const or non-const), temporary objects, or literal constants

Note: When a function argument is const, T is not a const type

F2 (i) I (int) T int

F2 (CI) CI (const int) T INT

F2 (5) error, not lvalue T int

Note: The const parameter can be bound to a right value


2) rvalue Reference


t&&

------Access Argument T

Lvalue lvalue Reference

Right- value Object type

Lvalue references lvalue reference

Rvalue reference rvalue reference

Reference folding can only be used to indirectly create references to references.

Common uses for the right direction: 1) template forwarding, template overloading


5 Std::move Principle

Template <typename t>

TypeName remove_reference<t>::type&& Move (t&& T) {

Reurn static_cast<typename remove_reference<t>::type&&> (T);

}

Note: Turning an lvalue static_cast to an rvalue reference is allowed.

6 Forwarding Std::forward

forward<t> return type is t&&








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + template argument inference

Related Article

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.