Constexpr and constant Expressions (c++11 standard)

Source: Internet
Author: User

constant Expressions (const expression): An expression that means that the value does not change and that the results are evaluated during compilation . (a constant expression cannot be a result of the run ).

const int i=3; is a constant expression

const int j=i+1; is a constant expression

int k=23; The value of K can be changed, thus not a constant expression

const int m=f (); is not a constant expression, the value of M is only obtained at run time.

constexpr Variables :

C++11 allows you to declare a constexpr type because the compiler verifies that the value of the variable is a constant expression. A constant must be declared as constexpr and can only be initialized with a constant or constant expression .

constexpr int i=3;

constexpr int j=i+1;

constexpr int k=f ();//Only F () is a constexpr function when k is a constant expression

In general, if the variable is a constant expression, declare it as a constexpr type

Constexpr and constant Expressions (c++11 standard)

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.