public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/112590] New: structural constexpr class fails to instantiate
@ 2023-11-17 15:26 janezz55 at gmail dot com
  2023-11-17 17:46 ` [Bug c++/112590] " mital at mitalashok dot co.uk
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: janezz55 at gmail dot com @ 2023-11-17 15:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112590
           Summary: structural constexpr class fails to instantiate
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janezz55 at gmail dot com
  Target Milestone: ---

Compiling:

#include <cstddef>

template <auto C> void test() {}

template <std::size_t N>
struct S
{
  int a_[N];
  int* f_;
  constexpr S(): a_{}, f_{a_} {}
};

int main()
{
  test<S<10>{}>();
  return 0;
}

fails with:

prog.cc: In function 'int main()':
prog.cc:18:16: error: no matching function for call to 'test<S<10>()>()'
   18 |   test<S<10>{}>();
      |   ~~~~~~~~~~~~~^~
prog.cc:4:6: note: candidate: 'template<auto C> void test()'
    4 | void test()
      |      ^~~~
prog.cc:4:6: note:   template argument deduction/substitution failed:
prog.cc:18:16: error: 'S<10>{int [10](), ((int*)(&<anonymous>.S<10>::a_))}' is
not a constant expression
   18 |   test<S<10>{}>();
      |   ~~~~~~~~~~~~~^~
prog.cc:18:16: error: 'S<10>()' is not a constant expression because it refers
to an incompletely initialized variable

But everything is initialized. I believe a workaround exists, but I didn't
experiment.

https://wandbox.org/permlink/xA81TIzI5XsXdh57

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

* [Bug c++/112590] structural constexpr class fails to instantiate
  2023-11-17 15:26 [Bug c++/112590] New: structural constexpr class fails to instantiate janezz55 at gmail dot com
@ 2023-11-17 17:46 ` mital at mitalashok dot co.uk
  2023-11-17 18:42 ` janezz55 at gmail dot com
  2023-11-17 19:26 ` janezz55 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: mital at mitalashok dot co.uk @ 2023-11-17 17:46 UTC (permalink / raw)
  To: gcc-bugs

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

Mital Ashok <mital at mitalashok dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mital at mitalashok dot co.uk

--- Comment #1 from Mital Ashok <mital at mitalashok dot co.uk> ---
See [temp.arg.nontype]p3
<https://timsong-cpp.github.io/cppwp/n4868/temp.arg.nontype#3>:

> For a non-type template-parameter of reference type, or for each non-static
> data member of reference or pointer type in a non-type template-parameter
> of class type or subobject thereof, the reference or pointer value shall
> not refer to or be the address of (respectively):
>  - A temporary object,
>  - [...]
>  - a subject of one of the above.

And "f_{a_}" is initializing the pointer as a subobject of a temporary
object, so this is invalid. Though the error given might be wrong or
misleading.

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

* [Bug c++/112590] structural constexpr class fails to instantiate
  2023-11-17 15:26 [Bug c++/112590] New: structural constexpr class fails to instantiate janezz55 at gmail dot com
  2023-11-17 17:46 ` [Bug c++/112590] " mital at mitalashok dot co.uk
@ 2023-11-17 18:42 ` janezz55 at gmail dot com
  2023-11-17 19:26 ` janezz55 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: janezz55 at gmail dot com @ 2023-11-17 18:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Janez Zemva <janezz55 at gmail dot com> ---
Very nice, but if I write:

int main()
{
  static constexpr S<10> s;
  return 0;
}

there will still be a compile error.

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

* [Bug c++/112590] structural constexpr class fails to instantiate
  2023-11-17 15:26 [Bug c++/112590] New: structural constexpr class fails to instantiate janezz55 at gmail dot com
  2023-11-17 17:46 ` [Bug c++/112590] " mital at mitalashok dot co.uk
  2023-11-17 18:42 ` janezz55 at gmail dot com
@ 2023-11-17 19:26 ` janezz55 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: janezz55 at gmail dot com @ 2023-11-17 19:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Janez Zemva <janezz55 at gmail dot com> ---
Sorry for my last comment. I have prepared a more involved example:

https://github.com/user1095108/ca/blob/master/consttests.cpp

If

test<a>();

is commented out, everything compiles, otherwise not.

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

end of thread, other threads:[~2023-11-17 19:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-17 15:26 [Bug c++/112590] New: structural constexpr class fails to instantiate janezz55 at gmail dot com
2023-11-17 17:46 ` [Bug c++/112590] " mital at mitalashok dot co.uk
2023-11-17 18:42 ` janezz55 at gmail dot com
2023-11-17 19:26 ` janezz55 at gmail dot com

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).