Find out the parameter is constexpr

前端 未结 3 1863
走了就别回头了
走了就别回头了 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:34

    Within the function, it isn't possible to check the parameters to see if the complete calling expression is a constant expression. But you can implement a macro at the call site to test the same thing, evaluating to true_type or false_type at compile time depending on whether the expression is a constant expression

    IS_A_CONSTANT_EXPRESSION(  fibo(5)          )    // is constant
    IS_A_CONSTANT_EXPRESSION(  fibo(time(NULL)  )    // is not constant
    

    The details are in this answer to another question. (My own answer, apologies for cross posting!)

    You could then implement another macro, FIBO( expr ) to wrap this up nicely and call the correct version as appropriate.

提交回复
热议问题