https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101828
Bug ID: 101828
Summary: The invocation of a virtual function for an object
that is not usable in constant expression is accepeted
by GCC
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: xmh970252187 at gmail dot com
Target Milestone: ---
struct A{
constexpr int virtual fun() const{
return 0;
}
};
A obj;
constexpr A const& rf = obj;
constexpr int v = rf.fun();
According to [expr.const#5.5]
> An expression E is a core constant expression unless the evaluation of E,
> following the rules of the abstract machine ([intro.execution]), would
> evaluate one of the following:
>> an invocation of a virtual function for an object unless
>>> the object is usable in constant expressions or
>>> its lifetime began within the evaluation of E;
Since `obj` is not an object that is usable in constant expression as per
[expr.const#4] and its lifetime has begun before the declaration of `v`, hence
its full-expression of the initialization cannot be a constant expression. It
should be ill-formed. https://godbolt.org/z/8hKWs3WEf