http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53792
Bug #: 53792 Summary: [C++11][constexpr] improving compiler-time constexpr evaluation Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: vincenzo.innoce...@cern.ch Filed under c++ even if it is most probably an optimization issue. At the moment it looks like that constexpr are evaluated at compile time only if explicitly assigned to a constexpr constant. There are cases though where the compiler can infer that the expression can still be evaluated at compile time even if used in a run-time context. Take the following quite realistic example of a consexpr "indexing table" used to access a non-const array using string literals though an inline function. In principle foo and bar are equivalent. At the moment gcc evaluates "getIndex" at compile time for bar (where the marco expansion explicitly instantiates a constexpr int, while it generates runtime code for foo that uses the inlined function getV. Would the compiler be able to transform getV in something like the code in the macro? constexpr entry theMap[] = { {"a", 0}, {"b", 1}, {nullptr,2} }; // filled at run time double v[3]; constexpr bool same(char const *x, char const *y) { return !*x && !*y ? true : (*x == *y && same(x+1, y+1)); } constexpr int getIndex(char const *label, entry const *entries) { return !entries->label ? entries->index : same(entries->label, label) ? entries->index : getIndex(label, entries+1); } inline double __attribute__((always_inline)) getV(const char * name ) { return v[getIndex(name,theMap)]; } #define SetV(X,NAME) \ constexpr int i_##X = getIndex(NAME, theMap);\ const double X = v[i_##X] int foo() { const double a = getV("a"); const double b = getV("b"); if (a==b) return 1; return 0; } int bar() { SetV(a,"a"); SetV(b,"b"); if (a==b) return 1; return 0; }