When does the constructor call const-expr?

When does the constructor call the const-expr constructor , and when not? In this question link

Are these vector definitions "permanent initialization"?

The constructor is not const-expr and therefore vector not const initialized. Also can someone explain the answer to this question in more detail?

+4
source share
1 answer

You ask, when can the result of construction be used in a context that requires constant expression? For example.

class A {
    constexpr A(...) { ... };
    constexpr int get() { ... };
    ...
}

constexpr A a(...);
std::array<double, a.get()> x{};

, ? std::array (http://en.cppreference.com/w/cpp/language/constant_expression) integer, get is constexpr, a constexpr. , constexpr ( , : http://en.cppreference.com/w/cpp/language/constexpr). , a ( , "constexpr" ), , , , , constexpr.

:

struct A {
  constexpr A(int x) : m_x(x) {};
  constexpr int get() { return m_x; }
  int m_x;
};

int main(int, char**)
{
  constexpr int x = 5;
  constexpr A a(x);
  std::array<double, a.get()> y;
  return 0;
}

, x constexpr const, . , constexpr, , , a , , a.get() . , , : - , , . , ( ) constexpr, , ( 5, constexpr).

+1

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


All Articles