-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconstexpr.cpp
More file actions
32 lines (27 loc) · 800 Bytes
/
constexpr.cpp
File metadata and controls
32 lines (27 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//*****************************************************************************
//
// Author: Michael Price
// License: Attribution-NonCommercial-NoDerivs 3.0 Unported
// http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode
//
//*****************************************************************************
constexpr double circle_area (double radius)
{ return 3.14 * radius * radius; }
class CircleArea
{
public:
constexpr CircleArea (double radius)
: m_area(circle_area(radius))
{ }
private:
double m_area;
};
void area_computations ()
{
// Calculated at compile-time!
constexpr double a1 = circle_area(2.0);
constexpr CircleArea ca(2.5); // Compile-time ctor!
// Calculated at run-time
double r2 = 3.0;
double a2 = circle_area(r2);
}