Why an enumeration instead of a static bool?

Why is it considered better to use enum not static const bool in template metaprogramming?
I read it somewhere in the book of Alexandrescu, but I can not find it and really would like to know.

+3
source share
3 answers

The main reason is that the static bool is a variable, and enum is a type - therefore, in the case of enum, no variable is ever created and, therefore, an estimate of the compilation time is guaranteed.

Also see this question for more details.

+7
source

++ (ISO/IEC 14882: 2003) const bool , .

pre-standard ++ ( ) . ++ . , , :

struct C
{
  static const int N = 10;
};
char data[C::N]; // N "used" without out-of-class definition

N.

, ++ 1998 , . , , sizeof typeid, .

, , , , . , case, nontype.

struct C
{
  static const int N = 10;
  static const int U = N; // Legal per C++03
};

char data[C::N]; // Legal per C++03

template<int> struct D;

template<> struct D<C::N> {}; // Legal per C++03

, , , . :

struct C
{
  static const int N = 10;
};

int main()
{
  int i = C::N; // ill-formed, definition of C::N required
}

, , .

+7

, , static const . static bool const - .

++ 03, §9.4.2/4:

static const integer const enumeration, , (5.19). . , , .

+3
source

Source: https://habr.com/ru/post/1795421/


All Articles