C + + 11 Travels of Decltype constexpr

Source: Internet
Author: User
Tags volatile

Title:c++ 11 Travels 1
Keyword:c++ Decltype constexpr

Titer1 Zhangyu
Source: www.drysaltery.com
Contact: 13,073,161,968 (SMS only)
Disclaimer: This document is licensed under the following protocols: Free reprint-Non-commercial-non-derivative-retention Attribution | Creative Commons by-nc-nd 3.0, reproduced please specify the author and source.
Tips for image:http://7xjs3n.com1.z0.glb.clouddn.com

C + + 11 travels 1 (Decltype constexpr) A. Decltype

Reference source

On code
#include <iostream>structADoublex;};ConstA * a =NewA ();Decltype(a->x) X3;//Type of X3 is double (declared type)Decltype((a->x)) x4 = x3;//Type of x4 is const double& (lvalue expression)Template<classTclassU>AutoAdd (T T, u u)Decltype(t + u);//return type depends on template parametersintMain () {inti = -;Decltype(i) j = i*2;STD::cout<<"i ="<< I <<", "<<"J ="<< J <<' \ n ';Autof = [] (intAintb)int{returnA*b; };Decltype(f) F2{f};//The type of a lambda function is unique and unnamedi = f (2,2); j = F2 (3,3);STD::cout<<"i ="<< I <<", "<<"J ="<< J <<' \ n ';}
Preliminary experience

The main points of this theory:
-Master four cases, see the code in detail
-return type for function post
-Variable (without parentheses) declaration
-Variable (with parentheses) declaration
-Correlation with LAMDA expressions
-
-It is important to note that if the name of an object is enclosed in parentheses, it becomes an lvalue expression

    • He is also a good friend of LAMDA expression!
      decltype is useful when declaring types that are difficult or impossible to declare using standard notation, like lambda-related types or types that depend on template parameters.
Try difficult points

If expression is a function call which returns a Prvalue of class type or was a comma expression whose right operand is suc H a function call, a temporary object was not introduced for that prvalue. The class type need not is complete or has an available destructor. This rule doesn ' t apply to Sub-expressions:in Decltype (f (g ())), G () must has a complete type, but F () need not.

Debug condition

I tried to change the arguments in the LAMDA expression, but the compiler prompted me to not pass, for the reasons to be identified

Two. constexpr
    • First, understand the literal value literaltype, the common phrase string

    • The value of an expression can be evaluated passively at compile time

    • CONSTEXPR extends the concept of compile-time constants to user-defined constants and constant functions, whose values are not modifiable by the compiler, so that constexpr expressions are generalized, guaranteed constant expressions
      比const 前置修饰的函数 的能力 更广
Code from CSDN
    enumFlags {good=0, fail=1, bad=2, eof=4};constexpr int operator|    (Flags F1, flags F2) {returnFlags (int(F1) |int(F2)); }voidF (Flags x) {Switch(x) { CaseBad/ * ... * /  Break; CaseEof:/ * ... * /  Break; CaseBad|eof:/ * ... * /  Break;default:/ * ... * /  Break; }    }constexpr intx1 = bad|eof;//OK    voidF (Flags F3) {//Error: Because F3 is not a constant, the result value of this expression cannot be evaluated at compile time        constexpr intx2 = bad|f3;intx3 = bad|f3;//OK, can be calculated at run time}
Code from cpp.com
#include <iostream>#include <stdexcept>//The c++11 constexpr functions use recursion rather than iteration//(c++14 constexpr functions may use local variables and loops)constexpr intFactorial (intN) {returnN <=1?1: (n * factorial (n1));}//Literal classclassCONSTSTR {Const Char* p;STD:: size_t sz; Public:Template<STD:: size_t n>constexprCONSTSTR (Const Char(&a) [n]): P (a), SZ (N-1) {}//CONSTEXPR functions signal errors by throwing Exceptions    //In c++11, they must does so from the conditional operator?:    constexpr Char operator[](STD:: size_t N)Const{returnN < sz? P[n]:Throw STD:: Out_of_range (""); }constexpr STD:: size_t size ()Const{returnSz }};//C++11 constexpr functions had to put everything in a single return statement//(c++14 doesn ' t has that requirement)constexpr STD:: size_t countlower (Conststr s,STD:: size_t n =0,STD:: size_t C =0) {returnn = = S.size ()? C:s[n] >=' A '&& S[n] <=' Z '? Countlower (S, n+1, c+1): Countlower (S, n+1, c);}//Output function that is requires a Compile-time constant, for testingTemplate<intN>structCONSTN {constn () {STD::cout<< N <<' \ n '; }};intMain () {STD::cout<<"4! = "; Constn<factorial (4) > OUT1;//computed at compile time    volatile intK =8;//Disallow optimization using volatile    STD::cout<< k <<"! = "<< factorial (k) <<' \ n ';//computed at run time    STD::cout<<"Number of lowercase letters in \" Hello, world!\ "is"; Constn<countlower ("Hello, world!.") > Out2;//implicitly converted to Conststr}
Experience
    • The value of the iteration function is computed during compilation!!!!!
    • There is, in detail, as follows:
Three. Other: good information

Scott Meyer speaking Cpp11

C++11 FAQ Chinese Version: constant expression (constexpr)
CPP I think one of the best information

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

C + + 11 Travels of Decltype constexpr

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.