constexpr
Welcome to the home of the ISO C++0x constexpr feature as implemented in GCC. Say yes to magic! For more details on this feature, also called generalized constant expressions, see N2235 for background and language changes and N2976 for library changes .
Git it
Development is hosted in git. Try it, you'll like it.
Starting from a git working tree as described in GitMirror you can set up to use the constexpr repository as follows:
git checkout -b constexpr origin/constexpr
To stay up to date, use:
git pull
Status
2010-11-02
This branch has been merged to trunk: all future work on constexpr will be part of the usual gcc efforts.
The branch merging, in patches:
C++0x constexpr PATCH #1: improve setting of CLASSTYPE_LITERAL_P
C++0x constexpr PATCH #2: improve type checking of constexpr declarations
C++0x constexpr patch #3: implicit constexpr
C++0x constexpr PATCH #4: is_literal_type
C++0x constexpr PATCH #5: two more small tweaks
C++0x constexpr PATCHes #7-10: the rest of the compiler support
C++0x constexpr PATCHes #11-13: library usage
Since the merge, many bugs have been fixed. Please use gcc trunk, not the constexpr branch, if you care about constexpr and wish to play with it.
G++:
See bugzilla query for current status http://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=constexpr
LIBSTDC++:
Notes:
There is an attempt to communicate the extensions in the GNU implementation to the ISO C++ LWG. That paper will be in the post-Batavia meeting mailing, and the draft can be previewed now. Constexpr Library Additions
1) <chrono>
Remove the following:
- - Remove explicitly defaulted duration default constructor, and add default constructor that can initialize possible member data with an initializaiton list.
- (As constexpr default constructors that are explicitly defaulted are only well formed for classes where all member data have constexpr default constructors.)
Add the following:
- Mark comparison operator templates ==, !=, <, <=, >, >= for duration constexpr - Mark all constructors in time_point constexpr - Mark member function time_point::time_since_epoc constexpr - Mark function templates duration_cast and timepoint_cast constexpr
- Mark comparison operator templates ==, !=, <, <=, >, >= for time_point constexpr - Mark system_clock::is_monotonic, monotonic_clock::is_monotonic, and high_resolution_clock::is_monotonic constexpr.
2) <bitmask>, Done, except for bool operator[], may not be able to be constexpr. NEEDS SPECIFICATION
3) <ios> ios_base:: bitmask types new operators are not constexpr, don't know if this is intended. This requires a signature change from ref to value.
4) <atomic> atomic_integral::is_atomic could be constexpr, except this changes some design decisions about compile-time vs. run-time. Current design is for run-time resolution. Note, some unresolved store/load const issues. Meta picture of how const variables interact needs clarification, tangential. Needs updating to Adjusting C++ Atomics for C Compatibility
5) <initializer_list> initializer_list members are all constexpr in implementation, GNU extensions
- - Mark class template initializer_list’s default constructor and member functions size, begin, and end constexpr. - Mark function templates begin and end constexpr.
7) <regex>. As done as regex. std::regex_constants needs some normalization WRT bitmask types (same as ios_base). Testsuite for random seems to be numbered and have other oddities.
8) <array>, seems like most of this could be constexpr
9) <string> basic_string's npos, size/length/max_size could be constexpr
10) <complex>
Add the following:
- - Mark primary template constructors and real() and imag() member functions constexpr. - Mark complex operators == and != constexpr. - Mark function templates real() and imag() constexpr.
11) <utility>
Add the following:
- - Mark primary template constructors of pair constexpr.
- Mark pair comparison operators ==, !=, >, >=, <, <= constexpr.
12) <future>
13) <tuple> make more ctors constexpr. Do it do it do it.
Add the following:
- - Mark default constructors of future constexpr. - Mark default constructors of shared_future constexpr.
META 1: Use of constexpr and some form of exception specifications, either throw specifications or noexcept. It turns out this is not redundant: constexpr functions can throw.
Jason says: A constant expression is noexcept even if it involves a call to a constexpr function that isn't declared noexcept.
Here's an example of a valid constexpr that throws:
constexpr int f (int i) { return i > 0 ? i : throw 42; }
META-2: Use of constexpr specifier with default constructors vs. defaulted constructors.
META-3: Opportunistic use of constexpr on member functions of class templates that are literal types, and boolean comparison operators of literal types.
META-4: Use of constexpr and const in member functions. Some places have both: this is redundant. Pick one, assume the one picked will be constexpr.
META-5: Opportunistic use of constexpr on constructors of class templates of non-literal type to make static initialization explicitly constant (and not dynamic.)
META-6: Static const vs. Static constexpr. Pick static constexpr.