举个例子:
class something {
public:
static constexpr int seconds(int hour, int min, int sec)
{ return hour*3600+min*60+sec; }
}
Run Code Online (Sandbox Code Playgroud)
然后:
printf("Look at the time: %d\n", something::seconds(10, 0, 0));
Run Code Online (Sandbox Code Playgroud)
将编译为使用g ++调用函数,而不是使用常数.为什么g ++会这样做?它没有任何好处,有点挫败了使用constexpr而不是糟糕的宏的目的.