Date: Fri, 20 Nov 2026 13:02:55 +0100
On 11/19/26 10:20, Cell via Std-Proposals wrote:
> Hello,
>
> I would like to discuss whether in-class initializers of inline static data
> members should be considered complete-class contexts.
>
> Currently, [*class.mem.general
> <https://eel.is/c%2B%2Bdraft/class.mem#general-10>*] treats function bodies
> and default member initializers as complete-class contexts, but not static
> data member initializers. This creates some restrictions even though C++17
> allows inline static data members to be defined and initialized within a
> class.
>
> For example, I would like the following to be well-formed:
>
> struct X { static constexpr std::size_t m_foo = alignof(S);
> static constexpr std::size_t m_bar = bar();
> static inline std::size_t m_baz = m_foo * 2;
> static inline int m_qux = qux();
>
> static consteval std::size_t bar() {
> return sizeof(S) * 2;
> }
> static int qux() {
> return 0;
> }
> };
>
> Ideally, all inline static data member initializers permitted within a
> class definition would be able to use the complete enclosing class, while
> retaining the existing requirements for constant expressions and the
> existing initialization-ordering rules.
>
> Are there fundamental semantic or implementation concerns with such an
> extension?
It creates the possibility of infinite recursion at compile time:
struct X {
static constexpr std::size_t m_bar = bar(); // currently illegal
static consteval std::size_t bar() {
return sizeof(X) * 2; // currently legal
}
char v[m_bar]; // currently legal
};
However, the possibility of infinite compile-time recursion isn't a new
thing. For example:
// legal until X is instantiated
template <bool B> struct X : X<!B> {
};
Or even:
// legal so long as n > 0, otherwise infinite recursion
constexpr int factorial(int n) {
if (n > 0) {
return factorial(n - 1) * n;
} else if (n == 0) {
return 1;
} else {
return factorial(n);
}
}
Adding yet another possibility for compile-time infinite recursion makes
writing compilers a bit more difficult, but it doesn't break anything
that isn't already broken.
> Hello,
>
> I would like to discuss whether in-class initializers of inline static data
> members should be considered complete-class contexts.
>
> Currently, [*class.mem.general
> <https://eel.is/c%2B%2Bdraft/class.mem#general-10>*] treats function bodies
> and default member initializers as complete-class contexts, but not static
> data member initializers. This creates some restrictions even though C++17
> allows inline static data members to be defined and initialized within a
> class.
>
> For example, I would like the following to be well-formed:
>
> struct X { static constexpr std::size_t m_foo = alignof(S);
> static constexpr std::size_t m_bar = bar();
> static inline std::size_t m_baz = m_foo * 2;
> static inline int m_qux = qux();
>
> static consteval std::size_t bar() {
> return sizeof(S) * 2;
> }
> static int qux() {
> return 0;
> }
> };
>
> Ideally, all inline static data member initializers permitted within a
> class definition would be able to use the complete enclosing class, while
> retaining the existing requirements for constant expressions and the
> existing initialization-ordering rules.
>
> Are there fundamental semantic or implementation concerns with such an
> extension?
It creates the possibility of infinite recursion at compile time:
struct X {
static constexpr std::size_t m_bar = bar(); // currently illegal
static consteval std::size_t bar() {
return sizeof(X) * 2; // currently legal
}
char v[m_bar]; // currently legal
};
However, the possibility of infinite compile-time recursion isn't a new
thing. For example:
// legal until X is instantiated
template <bool B> struct X : X<!B> {
};
Or even:
// legal so long as n > 0, otherwise infinite recursion
constexpr int factorial(int n) {
if (n > 0) {
return factorial(n - 1) * n;
} else if (n == 0) {
return 1;
} else {
return factorial(n);
}
}
Adding yet another possibility for compile-time infinite recursion makes
writing compilers a bit more difficult, but it doesn't break anything
that isn't already broken.
-- Rainer Deyke - rainerd_at_[hidden]
Received on 2026-11-20 12:03:06
