std::max() and std::min() not constexpr

后端 未结 5 1837
臣服心动
臣服心动 2020-12-10 23:31

I just noticed that the new standard defines min(a,b) and max(a,b) without constexpr.

Examples from 25.4.7, [

5条回答
  •  轮回少年
    2020-12-11 00:22

    min and max are only constant expressions if you call them with constant expressions as arguments. Since they're meant to be much more generally usable than that, you can't make the declaration.

    Here's what Wikipedia says about constexpr (emphasis added). I know Wikipedia is not the ultimate reference, but I believe it's correct in this case.

    The use of constexpr on a function imposes very strict limitations on what that function can do. First, the function must have a non-void return type. Second, the function contents must be of the form: return expr. Third, expr must be a constant expression, after argument substitution. This constant expression may only call other functions defined as constexpr, or it may use other constant expression data variables.

提交回复
热议问题