vendredi 2 janvier 2015

Is it a conforming compiler extension to treat non-constexpr standard library functions as constexpr?

gcc compiles the following code without warning:



#include <cmath>

struct foo {
static constexpr double a = std::cos(3.);
static constexpr double c = std::exp(3.);
static constexpr double d = std::log(3.);
static constexpr double e1 = std::asin(1.);
static constexpr double h = std::sqrt(.1);
static constexpr double p = std::pow(1.3,-0.75);
};

int main()
{
}


although none of these standard library functions are constexpr functions, we are allowed to use them where a constant expression is required from section 7.1.5 [dcl.constexpr]:



[...]If it is initialized by a constructor call, that call shall be a constant expression (5.19). Otherwise, or if a constexpr specifier is used in a reference declaration, every fullexpression that appears in its initializer shall be a constant expression.[...]



Even when using -std=c++14 -pedantic or -std=c++11 -pedantic no warnings are generated (see it live ). Using -fno-builtin produces errors which indicates that the builtin version of these standard library functions are being treated as if they where constexpr


While clang does not allow the code with any combination of flags I have tried.


So this is a gcc extension to treat at least some builtin functions as if they were constexpr functions even though the standard does not explicitly require them to be. I would have expected to at least receive a warning in strict conformance mode, is this a conforming extension?


Aucun commentaire:

Enregistrer un commentaire