CS253: Software Development with C++

Spring 2018

Constexpr

See this page as a slide show

CS253 Constexpr

constexpr

Examples

constexpr auto pi = 3.14159;
cout << pi << '\n';
3.14159
constexpr double avo=6.022e23;
avo = 1.234;
cout << avo;
c.cc:2: error: assignment of read-only variable 'avo'

Trying to cheat

Let’s try to fool constexpr:

constexpr int answer = 42;
int *p = &answer;
*p = 8675309;
cout << answer << '\n';
c.cc:2: error: invalid conversion from 'const int*' to 'int*'
constexpr int answer = 42;
int *p = const_cast<int *>(&answer);
*p = 8675309;
cout << *p << ' ' << answer << '\n';
8675309 42

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:51

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building