Find out the parameter is constexpr

前端 未结 3 1868
走了就别回头了
走了就别回头了 2021-01-23 04:54

Any type_traits or method could find out the parameters is a constexpr or not?

Example

size_t fibo_runtime(size_t num)
{
  //implementation
}

constexpr          


        
3条回答
  •  故里飘歌
    2021-01-23 05:48

    The very nature of constant expression (constexpr) is to be constant. So runtime evaluation or not, they will not change any of their behavior and the evaluation will never be done at runtime.

    If you use a constexpr function at runtime (which is not simply by calling it straightforwardly), then it will be called as a normal function, as the expression cannot be resolved by nature as constant during runtime.

    If you want to achieve some different implementation depending on compile-time or runtime it will (if you achieve it) be really disturbing for the developer as the function would not have the same behavior depending on how you call it ! Thus it becomes clear that such behavior could not / will not / should not be implemented.

    If you want to specialize a function for compile-time computation, use a different function to clearly state your intentions.

提交回复
热议问题