"C + + Primer The sixth chapter" constexpr function

Source: Internet
Author: User

constexpr function

constexpr function: The constexpr function refers to a function that is used for a constant expression, the return value type of the function, and all parameter types must be literal, and the function must have only one return statement.

1 [Tect2.cpp]2#include <iostream>3 using namespaceStdl;4 5constexprintScreenintX//constexpr6 {7    returnx;8 }9 Ten intMain () One {    A     Const intx =0;//Const, which is a constant expression because the value does not change -constexprintz = screen (x);//constexpr -cout << z<<Endl; the     return 0; -}
1 [Tect2.cpp]2 3#include <iostream>4 using namespacestd;5 6constexprintScreenintx)7 {8    returnx;9 }Ten  One  A intMain () - {    -     intx =0;//is not a constant expression because the value changes the     intz =Screen (x); -cout << z<<Endl; -     return 0; -}

The analysis begins by explaining the definition of a constant expression: a constant expression is an expression in which the value does not change and the result can be obtained at compile time.
(1) [test1.cpp] conforms to the general usage of the CONSTEXPR function, that is, the return type of the function and all formal parameter types must be literal types (the literal type is the type of the result that the compilation process can get). At this point, if the const int x = 0; an int x = 0 will be an error because the screen function is used in a context where constant expressions are required (variables of type constexpr must be initialized with constant expressions), and the compiler will check that the return value of the function is not a constant expression during compilation, and if not, it will be an error.
(2) [test2.cpp] means that the CONSTEXPR function can return a non-const expression, the compilation process is not an error, because the screen function is not used in a context that requires constant expression, the compiler is compiling the process does not check the function's return value, will not error.
(3) as a supplement, it is important to note that the CONSTEXPR function must have a return statement.


Conclusion : It can be argued that C + + does not require the CONSTEXPR function to return a constant expression (take the screen constexpr function in question as an example)

• If the context of a constant expression is not required, such as: int z = screen (x); You can not return a constant expression, at which time the compiler does not check whether the result of the function returns a constant expression.
• In contexts where constant expressions are required, such as: constexpr int z = screen (x); Then, the CONSTEXPR function must be able to return a constant expression. At this point the compiler goes to check that the function returned is not a constant expression, and if not, it will be an error.

Resources

[1] segmentfault, a question about the constexpr function

"C + + Primer The sixth chapter" constexpr function

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.