public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101828] New: The invocation of a virtual function for an object that is not usable in constant expression is accepeted by GCC
@ 2021-08-09  9:53 xmh970252187 at gmail dot com
  2021-08-09 18:00 ` [Bug c++/101828] The invocation of a virtual function for an object that is not usable in constant expression is accepted " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: xmh970252187 at gmail dot com @ 2021-08-09  9:53 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/101828] The invocation of a virtual function for an object that is not usable in constant expression is accepted by GCC
  2021-08-09  9:53 [Bug c++/101828] New: The invocation of a virtual function for an object that is not usable in constant expression is accepeted by GCC xmh970252187 at gmail dot com
@ 2021-08-09 18:00 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-09 18:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101828

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-08-09
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
With -pedantic-errors -std=c++17, we do reject it:
<source>:2:19: error: member 'fun' can be declared both 'virtual' and
'constexpr' only in '-std=c++20' or '-std=gnu++20' [-Wc++20-extensions]
    2 |     constexpr int virtual fun() const{
      |     ~~~~~~~~~     ^~~~~~~


With -pedantic-errors -std=c++20 we don't reject this even though we should,
clang gives:
Output of x86-64 clang (trunk) (Compiler #2)
<source>:8:15: error: constexpr variable 'v' must be initialized by a constant
expression
constexpr int v = rf.fun();
              ^   ~~~~~~~~
<source>:8:22: note: virtual function called on object 'obj' whose dynamic type
is not constant
constexpr int v = rf.fun();
                     ^

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-09 18:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-09  9:53 [Bug c++/101828] New: The invocation of a virtual function for an object that is not usable in constant expression is accepeted by GCC xmh970252187 at gmail dot com
2021-08-09 18:00 ` [Bug c++/101828] The invocation of a virtual function for an object that is not usable in constant expression is accepted " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).