mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 03:04:36 +03:00
static constant floating point need constexpr
Prior to C++11, only static const data members of integral or enumeration type could have initializers in the class definition. C++11 extends that to floating point types (float, double) as well. However, in order for such members to be initialized in the class defintion, such members must either be constexpr or non-static.
This commit is contained in:
@@ -16,7 +16,7 @@ namespace my_project {
|
||||
// if the above macro would be expanded, then the following line would be:
|
||||
// static const double 3.14159 = 3.14159;
|
||||
// which leads to a compile-time error. Sometimes such errors are hard to understand.
|
||||
static const double PI = 3.14159;
|
||||
static constexpr double PI = 3.14159;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user