Calling member function before base subobject initialization in constant evaluation not rejected
Consider this program:
#include <cstddef>
struct A {
constexpr A(int x) : val(x) {}
int val;
};
struct B : A {
constexpr int f() const { return 42; }
constexpr B() : A(f()) {} // undefined behavior: calls member function f() but base A not yet initialized
};
constexpr int foo() {
constexpr B b{};
return b.val;
}
int main() {
constexpr auto x = foo();
return 0;
}
C++20 final draft N4861 [class.base.init]/16:
Member functions (including virtual member functions, 11.7.2) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (7.6.1.7) or of a dynamic_cast (7.6.1.6). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer ) before all the mem-initializer s for base classes have completed, the program has undefined behavior. [Example:
class A { public: A(int); }; class B : public A { int j; public: int f(); B() : A(f()), // undefined behavior: calls member function but base A not yet initialized j(f()) { } // well-defined: bases are all initialized };
So this is UB and should be rejected in constant evaluation. Neither GCC nor Clang rejects it. godbolt
You provided wrong godbolt link.
@zwuis Sorry about that. Fixed.
@llvm/issue-subscribers-clang-frontend
Author: GKxxUCAS
#include <cstddef>
struct A {
constexpr A(int x) : val(x) {}
int val;
};
struct B : A {
constexpr int f() const { return 42; }
constexpr B() : A(f()) {} // undefined behavior: calls member function f() but base A not yet initialized
};
constexpr int foo() {
constexpr B b{};
return b.val;
}
int main() {
constexpr auto x = foo();
return 0;
}
C++20 final draft N4861 [class.base.init]/16:
> Member functions (including virtual member functions, 11.7.2) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (7.6.1.7) or of a dynamic_cast (7.6.1.6). However, if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer ) before all the mem-initializer s for base classes have completed, the program has undefined behavior. [Example:
> cpp > class A { > public: > A(int); > }; > class B : public A { > int j; > public: > int f(); > B() : A(f()), // undefined behavior: calls member function but base A not yet initialized > j(f()) { } // well-defined: bases are all initialized > }; >
So this is UB and should be rejected in constant evaluation. Neither GCC nor Clang rejects it. godbolt
I have created a PR fixing this issue #219358. @GKxxUCAS
It's unclear what we should do here. For reference, I sent the following to CWG
Consider
struct A { constexpr A(int x) : val(x) {} int val; }; struct B : A { constexpr int f() const { return 42; } constexpr int g() const { return val; } constexpr B() : A(f()) {} // #1 }; constexpr int foo() { constexpr B b{}; return b.val; } constexpr auto x = foo();No implementation rejects that code. Presumably they should because in #1 a member function (f) is called during the evaluation of a constructor initializer before val is initialized. https://eel.is/c++draft/class.base.init#18.1 But because f() does not actually read any member, it is not rejected by any compiler. Similarly, I am not aware of any sanitizer handling this scenario.
Of course, if we replace the call to f by a call to g, every implementation diagnoses the read of an uninitialized member variable. So my question is, is that the intent of that wording, or should it be more narrowingly specified in terms of access to member / access to this ?
My current patch follows the broader interpretation and rejects the member call while the bases are being initialized, regardless of whether the function actually accesses any fields. If CWG decides that functions like f() should be allowed but g() should not, then I think the implementation would need to detect whether the evaluated function actually accesses the object's fields. Is that the direction you're expecting CWG to clarify? If so, I can look into what would be needed on the evaluator side.