constexpr int i = 1; // OK, definition constexpr extern int j; // Error, not a definition constexpr int f1(); // OK, function declaration, but must be defined before use 如果您声明的函数不是具有constexpr说明符的构造函数,那么该函数是constexpr函数。 同样,如果使用constexpr说明符声明构造...
不同于const,constexpr还可以修饰函数和类的构造函数。constexpr表示值或者返回值是常量,并且如果可能,...
// Recursive constexpr function constexpr int fac(int n) { return n == 1 ? 1 : n*fac(n - 1); } // User-defined type class Foo { public: constexpr explicit Foo(int i) : _i(i) {} constexpr int GetValue() { return _i; } private: int _i; }; int main() { // foo ...
{returnN; }// Recursive constexpr functionconstexprintfac(intn){returnn ==1?1: n * fac(n -1); }// User-defined typeclassFoo{public:constexprexplicitFoo(inti): _i(i){}constexprintGetValue()const{return_i; }private:int_i; };intmain(){// foo is const:constexprFoofoo(5);//...
constexprint*np=nullptr;// np is a constant pointer to int that is nullintj=0;constexprinti=42;// type of i is const int// i and j must be defined outside any function <==i和j必须定义在任何函数体之外constexprconstint*p=&i;// p is a constant pointer to the const int iconstexp...
{returnN; }// Recursive constexpr functionconstexprintfac(intn){returnn ==1?1: n * fac(n -1); }// User-defined typeclassFoo{public:constexprexplicitFoo(inti): _i(i){}constexprintGetValue()const{return_i; }private:int_i; };intmain(){// foo is const:constexprFoofoo(5);//...
I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...
constexpr.demo.cpp:2:5: note: ‘int g’ is not const int g; ^ constexpr.demo.cpp: At global scope: constexpr.demo.cpp:3:15: warning: inline function ‘constexpr int bar()’ used but never defined [enabled by default] constexpr int bar(); ...
constexpr.demo.cpp:2:5: note: ‘int g’ is not const int g; ^ constexpr.demo.cpp: At global scope: constexpr.demo.cpp:3:15: warning: inline function ‘constexpr int bar()’ used but never defined [enabled by default] constexpr int bar(); ...
as defined in 6.2.3), have no linkage, and have either block scope or function prototype ...