C + + constexpr type specifier

Source: Internet
Author: User


This series of articles by the @yhl_leo produced, reproduced please indicate the source.
Article Link: http://blog.csdn.net/yhl_leo/article/details/50864210

Keywords are constexpr introduced in c++11 and improved in c++14. It represents a constant expression. As with the const same, it can be applied to a variable, so a compiler error is thrown if any code attempts to modify the value. const constexpr can also be applied to functions and class constructors. constexprindicates that a value or return value is a constant and, if possible, evaluates or returns a value at compile time.

1 constexpr variables

constconstexprThe main difference between variables is that const initialization of variables can be deferred to runtime, and constexpr variables must be initialized at compile time. all constexpr variables are constants, so you must initialize them with a constant expression.

constexpr  float  x = 42.0 ; constexpr  float  y{108 }; constexpr  float  z = exp  (5 , 3 ); constexpr  int  i; //error! Not initialized  int  j = 0 ; constexpr  int  k = j + 1 ; //error! J not a constant expression   

In general, if you assume that a variable is a constant expression, declare it as a constexpr type.

2 constexpr functions

constexprA function is a function that evaluates its return value at compile time when it uses code that requires it. When its argument is a constexpr value and the code needs to be returned at compile time (for example, to initialize a constexpr variable or provide a non-type template parameter), it generates a compile constant. When you use a non- constexpr argument call, or when you do not need its value at compile time, it will generate a value at run time, just like a regular function.

#include <iostream>using namespace STD;//Pass by valueconstexpr float Exp(floatXintN) {returnn = =0?1N2==0?Exp(x * x, N/2) :Exp(x * x, (N-1) /2) * x;};//Pass by referenceconstexpr floatEXP2 (Const float& X,Const int& N) {returnn = =0?1N2==0? EXP2 (x * x, N/2): EXP2 (x * x, (N-1) /2) * x;};//Compile time computation of array lengthTemplate<TypeNameTintN>constexpr intLengthConstT (&ary) [N]) {returnN;}//Recursive constexpr functionconstexpr intFacintN) {returnn = =1?1: N*FAC (N-1);}//user-defined typeclassfoo{ Public:constexpr ExplicitFoo (inti): _i (i) {}constexpr intGetValue () {return_i; }Private:int_i;};intMain () {//foo is const:    constexprFoo Foo (5);//foo = foo (6);//error!    //compile Time:    constexpr floatx =Exp(5,3);//Const FLOAT a = 2.0;    //Const int b = 2;    //constexpr float xx = Exp2 (A, B);//error!    constexpr floatxx = EXP2 (2.0,2);constexpr floaty{Exp(2,5) };constexpr intval = foo. GetValue ();constexpr intF5 = FAC (5);Const intnums[]{1,2,3,4};Const intNums2[length (nums) *2]{1,2,3,4,5,6,7,8};cout<<"The value of Foo is"<< foo. GetValue () << Endl;}
3 constexpr and pointers

Remember const the rules with pointers? If the keyword const appears to the left of the asterisk, indicating that the object is a constant, and if it appears to the right of the asterisk, the pointer itself is a constant, and if it appears on both sides of the asterisk, both the finger and the pointer are constants.

Unlike const , in constexpr a declaration, if a pointer is defined, the qualifier is constexpr valid only for pointers, regardless of the object that the pointer refers to.

constint *p     0// non-const pointer, const dataconstexprint0// const pointer, non-const data

Like other constant pointers, const pointers can point to constants or to a very good amount:

int0;constexprint2;constexprconstint// const pointer, const dataconstexprint// const pointer, non-const data

C + + constexpr type specifier

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.