In a type trait, why do people use enum rather than static const for the value?

前端 未结 5 1857
别那么骄傲
别那么骄傲 2021-02-04 02:28

For example, this is how I would write it, and it compiles and works just fine:

template struct is_pointer {
  static const bool valu         


        
5条回答
  •  北海茫月
    2021-02-04 03:31

    Yes you are correct: enum { value = true }; doesn't occupy any memory.

    Furthermore, prior to C++11 it was pretty much the only way of achieving this: static const bool value = true; is only legal in a class definition from C++11 onwards. Although a constexpr might be preferred.

提交回复
热议问题