Date: Thu, 19 Nov 2026 17:20:19 +0800
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?
I would appreciate any thoughts or references to previous discussions.
Thanks.
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?
I would appreciate any thoughts or references to previous discussions.
Thanks.
Received on 2026-11-19 09:20:36
