public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function
@ 2024-03-11 18:44 carlo at gcc dot gnu.org
  2024-03-11 18:53 ` [Bug c++/114311] Possibly " carlo at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: carlo at gcc dot gnu.org @ 2024-03-11 18:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114311
           Summary: wrong "error: ‘this’ is not a constant expression"
                    with consteval member function
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlo at gcc dot gnu.org
  Target Milestone: ---

The following snippet:

```
template<int Id>
struct SymbolRef { };

struct Symbol {
  int const id_;

  consteval Symbol() : id_(1) { }

  template<int Id>
  consteval operator SymbolRef<Id>() const
  {
    return SymbolRef<id_>{};
  }
};
```

fails with error:

```
source>: In member function 'consteval Symbol::operator SymbolRef<Id>() const':
<source>:12:22: error: '*(const Symbol*)this' is not a constant expression
   12 |     return SymbolRef<id_>{};
      |                      ^~~
<source>:12:22: note: in template argument for type 'int'
```

Not a regression.

DOES compile with using `this->id_`.

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
@ 2024-03-11 18:53 ` carlo at gcc dot gnu.org
  2024-03-11 19:05 ` carlo at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: carlo at gcc dot gnu.org @ 2024-03-11 18:53 UTC (permalink / raw)
  To: gcc-bugs

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

Carlo Wood <carlo at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|wrong "error: ‘this’ is not |Possibly wrong "error:
                   |a constant expression" with |‘this’ is not a constant
                   |consteval member function   |expression" with consteval
                   |                            |member function

--- Comment #1 from Carlo Wood <carlo at gcc dot gnu.org> ---
I stand corrected. When adding `this->` it is still giving an error when trying
to instantiate the template member function.

For example:

```
template<int Id>
struct SymbolRef { };

struct Symbol {
  int const id_;

  consteval Symbol() : id_(1) { }

  template<int Id>
  consteval operator SymbolRef<Id>() const
  {
    return SymbolRef<this->id_>{};
  }
};

int main()
{
  Symbol x;

  static_cast<SymbolRef<1>>(x);
}
```

Gives an equivalent error, despite the `this->`.
I am not certain anymore if this is a wrong error.

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
  2024-03-11 18:53 ` [Bug c++/114311] Possibly " carlo at gcc dot gnu.org
@ 2024-03-11 19:05 ` carlo at gcc dot gnu.org
  2024-03-11 19:54 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: carlo at gcc dot gnu.org @ 2024-03-11 19:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Carlo Wood <carlo at gcc dot gnu.org> ---
I meant `constexpr Symbol x;` inside main - with the same error.

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
  2024-03-11 18:53 ` [Bug c++/114311] Possibly " carlo at gcc dot gnu.org
  2024-03-11 19:05 ` carlo at gcc dot gnu.org
@ 2024-03-11 19:54 ` redi at gcc dot gnu.org
  2024-03-11 20:48 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-11 19:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
MSVC compiles it but Clang and EDG agree with GCC:

$ clang++ c.cc -std=c++2b
c.cc:12:22: error: non-type template argument is not a constant expression
    return SymbolRef<this->id_>{};
                     ^~~~~~~~~
c.cc:12:22: note: use of 'this' pointer is only allowed within the evaluation
of a call to a 'constexpr' member function
c.cc:20:29: error: call to consteval function 'Symbol::operator SymbolRef<1>'
is not a constant expression
  static_cast<SymbolRef<1>>(x);
                            ^
c.cc:13:3: note: control reached end of constexpr function
  }
  ^
c.cc:20:29: note: in call to '&x->operator SymbolRef()'
  static_cast<SymbolRef<1>>(x);
                            ^
c.cc:20:3: warning: expression result unused [-Wunused-value]
  static_cast<SymbolRef<1>>(x);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 2 errors generated.


$ edg --c++20 c.cc
"c.cc", line 12: error: expression must have a constant value
      return SymbolRef<this->id_>{};
                       ^
"c.cc", line 12: note: the value of *this cannot be used as a constant
      return SymbolRef<this->id_>{};
                       ^

"c.cc", line 12: error: class "SymbolRef<1>" has no suitable copy constructor
      return SymbolRef<this->id_>{};
             ^
          detected during instantiation of "Symbol::operator SymbolRef<Id>()
                    const [with Id=1]" at line 20

2 errors detected in the compilation of "c.cc".

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-03-11 19:54 ` redi at gcc dot gnu.org
@ 2024-03-11 20:48 ` pinskia at gcc dot gnu.org
  2024-03-13  0:07 ` carlo at gcc dot gnu.org
  2024-03-13  0:13 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-11 20:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I thought I saw this before ...

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-03-11 20:48 ` pinskia at gcc dot gnu.org
@ 2024-03-13  0:07 ` carlo at gcc dot gnu.org
  2024-03-13  0:13 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: carlo at gcc dot gnu.org @ 2024-03-13  0:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Carlo Wood <carlo at gcc dot gnu.org> ---
Hey Andrew :)
You probably mean https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98752
But they are different in that I think that this report is invalid...
See "discussion" here: https://stackoverflow.com/a/78142737/1487069
But I'd definitely like to hear your expert view on it. If it's invalid then
sorry for wasting your time :/.

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

* [Bug c++/114311] Possibly wrong "error: ‘this’ is not a constant expression" with consteval member function
  2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-03-13  0:07 ` carlo at gcc dot gnu.org
@ 2024-03-13  0:13 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-13  0:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Carlo Wood from comment #5)
> Hey Andrew :)
> You probably mean https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98752
> But they are different in that I think that this report is invalid...
> See "discussion" here: https://stackoverflow.com/a/78142737/1487069
> But I'd definitely like to hear your expert view on it. If it's invalid then
> sorry for wasting your time :/.

No I am thinking of PR 114124 .

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

end of thread, other threads:[~2024-03-13  0:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-11 18:44 [Bug c++/114311] New: wrong "error: ‘this’ is not a constant expression" with consteval member function carlo at gcc dot gnu.org
2024-03-11 18:53 ` [Bug c++/114311] Possibly " carlo at gcc dot gnu.org
2024-03-11 19:05 ` carlo at gcc dot gnu.org
2024-03-11 19:54 ` redi at gcc dot gnu.org
2024-03-11 20:48 ` pinskia at gcc dot gnu.org
2024-03-13  0:07 ` carlo at gcc dot gnu.org
2024-03-13  0:13 ` 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).