{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);//...
but function is not marked const a.f2(); ^ a.cc:5:17: note: 'f2' declared here ...
{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);//...
其中比较感兴趣的类型:函数参数为PARM_DECL类型,可以看到是直接返回true的,也就是前面例子中的x变量;对于g、gc属于VAR_DECL类型,而bar()属于CALL_EXPR类型,通过该case最后的return true返回满足条件(这里补充一点,“bar()”这个是函数调用类型CALL_EXPR,而单独的“bar”是FUNCTION_DECL类型)。
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(); ...
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...
constexpr std::string_view foo("abc"); // C2131: expression did not evaluate to a constant constexpr std::string_view foo("abc", 3); // No error Looking deeper into the code, the root of the problem seems to be that the std::char_traits::length() function ...
constexpr extern int j; // Error, not a definition constexpr int f1(); // OK, function declaration, but must be defined before use 如果您声明的函数不是具有constexpr说明符的构造函数,那么该函数是constexpr函数。 同样,如果使用constexpr说明符声明构造函数,那么该构造函数是constexpr构造函数。constex...
#include <iostream> // The return value of a non-constexpr function is not a constant expression int five() { return 5; } int main() { constexpr double gravity { 9.8 }; // ok: 9.8 is a constant expression constexpr int sum { 4 + 5 }; // ok: 4 + 5 is a constant expressi...
In the Visual Studio debugger, when debugging a non-optimised Debug build, you can tell whether a constexpr function is being evaluated at compile time by putting a breakpoint inside it. If the breakpoint is hit, the function was called at run-time. If not, then the function was called...