public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
@ 2021-02-21  1:52 huili80 at gmail dot com
  2021-02-22 16:44 ` [Bug c++/99186] " m.cencora at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: huili80 at gmail dot com @ 2021-02-21  1:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99186
           Summary: std::tuple compilation error when elements are
                    specializations of template class declared with
                    template < auto E > syntax with E being a enumerator
                    of a enum
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: huili80 at gmail dot com
  Target Milestone: ---

Example code below:

#include <type_traits>
#include <tuple>

enum class E1 {a};
enum class E2 {b,c};

template < auto >
struct S
{
    int i;
};

static_assert(not std::is_same_v<S<E1::a>,S<E2::b>>);

struct D
: S<E1::a>, S<E2::b>
{};

int main()
{
   D d;
   d.S<E1::a>::i = 0;

   std::tuple<S<E1::a>,S<E2::b>,S<E2::c>> x;
   std::get<0>(x).i = 0;
   std::get<S<E2::c>>(x).i = 0;
   std::get<S<E2::b>>(x).i = 0; // does not compile
   return 0;
}


GCC 10.2 c++17 gives the following error:
In file included from main.cpp:2:
/usr/local/include/c++/10.2.0/tuple: In instantiation of 'constexpr _Tp&
std::get(std::tuple<_UTypes ...>&) [with _Tp = S<E2::b> _Types = {S<E1::a>,
S<E2::b>, S<E2::c>}]':
main.cpp:27:24:   required from here
/usr/local/include/c++/10.2.0/tuple:1339:37: error: no matching function for
call to '__get_helper2<S<E2::b> >(std::tuple<S<E1::a>, S<E2::b>, S<E2::c> >&)'
 1339 |     { return std::__get_helper2<_Tp>(__t); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
/usr/local/include/c++/10.2.0/tuple:1327:5: note: candidate: 'template<class
_Head, long unsigned int __i, class ... _Tail> constexpr _Head&
std::__get_helper2(std::_Tuple_impl<__i, _Head, _Tail ...>&)'
 1327 |     __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
      |     ^~~~~~~~~~~~~
/usr/local/include/c++/10.2.0/tuple:1327:5: note:   template argument
deduction/substitution failed:
/usr/local/include/c++/10.2.0/tuple:1339:37: note:   'std::_Tuple_impl<__i,
S<E2::b>, _Tail ...>' is an ambiguous base class of 'std::tuple<S<E1::a>,
S<E2::b>, S<E2::c> >'
 1339 |     { return std::__get_helper2<_Tp>(__t); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
/usr/local/include/c++/10.2.0/tuple:1332:5: note: candidate: 'template<class
_Head, long unsigned int __i, class ... _Tail> constexpr const _Head&
std::__get_helper2(const std::_Tuple_impl<__i, _Head, _Tail ...>&)'
 1332 |     __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t)
noexcept
      |     ^~~~~~~~~~~~~
/usr/local/include/c++/10.2.0/tuple:1332:5: note:   template argument
deduction/substitution failed:
/usr/local/include/c++/10.2.0/tuple:1339:37: note:   'const
std::_Tuple_impl<__i, S<E2::b>, _Tail ...>' is an ambiguous base class of
'std::tuple<S<E1::a>, S<E2::b>, S<E2::c> >'
 1339 |     { return std::__get_helper2<_Tp>(__t); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
Output:



If E1 and E2 had been non-scoped enums, the code results in the same compiler
error.

It's curious that the error mentions ambiguous base classes in std::tuple's
implementation. Given that std::get<S<E2::c>>(x) compiles fine, I speculated
that the compiler may have incorrectly thought that S<E1::a> and S<E2::b> are
the same type since E1::a and E2::b have the same numeric value (zero).
However, that speculation is proven wrong by other parts of the example code
clearly shows that the compiler does NOT think S<E1::a> and S<E2::b> are the
same type, e.g., the static_assert that S<E1::a> and S<E2::b> are not the same
type, and that even though struct D inherits from both S<E1::a>, S<E2::b>, we
can access its base S<E1::a>'s member variable.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
@ 2021-02-22 16:44 ` m.cencora at gmail dot com
  2021-08-12 21:33 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: m.cencora at gmail dot com @ 2021-02-22 16:44 UTC (permalink / raw)
  To: gcc-bugs

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

m.cencora at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |m.cencora at gmail dot com

--- Comment #1 from m.cencora at gmail dot com ---
This looks like a variation of
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93740

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
  2021-02-22 16:44 ` [Bug c++/99186] " m.cencora at gmail dot com
@ 2021-08-12 21:33 ` pinskia at gcc dot gnu.org
  2021-12-08 14:13 ` marxin at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-12 21:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Seems fixed in GCC 11.2.0 and on the trunk.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
  2021-02-22 16:44 ` [Bug c++/99186] " m.cencora at gmail dot com
  2021-08-12 21:33 ` pinskia at gcc dot gnu.org
@ 2021-12-08 14:13 ` marxin at gcc dot gnu.org
  2021-12-08 15:38 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-12-08 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|needs-bisection             |
   Last reconfirmed|                            |2021-12-08
             Status|UNCONFIRMED                 |NEW
                 CC|                            |marxin at gcc dot gnu.org
     Ever confirmed|0                           |1
      Known to work|                            |12.0

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
Fixed by r12-2327-g17855eed7fc76b2c.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-12-08 14:13 ` marxin at gcc dot gnu.org
@ 2021-12-08 15:38 ` redi at gcc dot gnu.org
  2021-12-08 15:49 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-08 15:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=93740

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The compiler problem isn't fixed, but I think comment 1 is correct that PR
93740 already covers that.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
                   ` (3 preceding siblings ...)
  2021-12-08 15:38 ` redi at gcc dot gnu.org
@ 2021-12-08 15:49 ` redi at gcc dot gnu.org
  2021-12-08 15:53 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-08 15:49 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|diagnostic                  |

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced to remove the library dependency:

template<int N, typename T, typename... U>
struct tuple_impl : tuple_impl<N + 1, U...>
{ };

template<int N, typename T>
struct tuple_impl<N, T>
{ };

template<typename T, typename U> struct tuple : tuple_impl<0, T, U> { };

template<typename T, int N, typename... U>
void
get(const tuple_impl<N, T, U...>&)
{ }

enum class E1 {a};
enum class E2 {b,c};

template < auto >
struct S
{
    int i;
};

int main()
{
   tuple<S<E1::a>,S<E2::b>> x;
   get<S<E2::b>>(x); // does not compile
}

Clang and EDG compile this, G++ has a bogus ambiguity:

ambig.C: In function ‘int main()’:
ambig.C:28:18: error: invalid initialization of reference of type ‘const
tuple_impl<0, S<E2::b>, S<E2::b> >&’ from expression of type ‘tuple<S<E1::a>,
S<E2::b> >’
   28 |    get<S<E2::b>>(x); // does not compile
      |                  ^
ambig.C:13:5: note: in passing argument 1 of ‘void get(const tuple_impl<N, T, U
...>&) [with T = S<E2::b>; int N = 0; U = {S<E2::b>}]’
   13 | get(const tuple_impl<N, T, U...>&)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The bug is that S<E1::a> and S<E2::b> are distinct types but the compiler gets
them confuses.

We have several bugs about template<auto> types that get confused.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
                   ` (4 preceding siblings ...)
  2021-12-08 15:49 ` redi at gcc dot gnu.org
@ 2021-12-08 15:53 ` redi at gcc dot gnu.org
  2023-12-13 20:57 ` cvs-commit at gcc dot gnu.org
  2023-12-16 17:15 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-08 15:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
And it doesn't matter whether enums are used:

template<int N, typename T, typename... U>
struct tuple_impl : tuple_impl<N + 1, U...>
{ };

template<int N, typename T>
struct tuple_impl<N, T>
{ };

template<typename T, typename U>
struct tuple : tuple_impl<0, T, U>
{ };

template<typename T, int N, typename... U>
void
get(const tuple_impl<N, T, U...>&)
{ }

template < auto >
struct S
{
    int i;
};

int main()
{
   tuple<S<1>,S<1U>> x;
   get<S<1U>>(x); // does not compile
}


The compiler doesn't correctly distinguish S<1> and S<1L>.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
                   ` (5 preceding siblings ...)
  2021-12-08 15:53 ` redi at gcc dot gnu.org
@ 2023-12-13 20:57 ` cvs-commit at gcc dot gnu.org
  2023-12-16 17:15 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-12-13 20:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:35ba3add7d0a9fc6ce955ba8ad82b0413e86ad7d

commit r14-6520-g35ba3add7d0a9fc6ce955ba8ad82b0413e86ad7d
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Dec 13 15:55:14 2023 -0500

    c++: unifying constants vs their type [PR99186, PR104867]

    When unifying constants we need to treat constants of different types
    but same value as different in light of auto template parameters since
    otherwise e.g. A<1> will unify with A<1u> (where A's template-head is
    template<auto>).  This patch fixes this in a minimal way; it seems we
    could get away with just using template_args_equal here, as we do in the
    default case, or even just cp_tree_equal since the CONVERT_EXPR_P loop
    seems to be dead code, but that's a simplification we could consider
    during next stage 1.

            PR c++/99186
            PR c++/104867

    gcc/cp/ChangeLog:

            * pt.cc (unify) <case INTEGER_CST>: Compare types as well.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1z/nontype-auto23.C: New test.
            * g++.dg/cpp1z/nontype-auto24.C: New test.

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

* [Bug c++/99186] std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum
  2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
                   ` (6 preceding siblings ...)
  2023-12-13 20:57 ` cvs-commit at gcc dot gnu.org
@ 2023-12-16 17:15 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-12-16 17:15 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |14.0

--- Comment #8 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 14

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

end of thread, other threads:[~2023-12-16 17:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-21  1:52 [Bug c++/99186] New: std::tuple compilation error when elements are specializations of template class declared with template < auto E > syntax with E being a enumerator of a enum huili80 at gmail dot com
2021-02-22 16:44 ` [Bug c++/99186] " m.cencora at gmail dot com
2021-08-12 21:33 ` pinskia at gcc dot gnu.org
2021-12-08 14:13 ` marxin at gcc dot gnu.org
2021-12-08 15:38 ` redi at gcc dot gnu.org
2021-12-08 15:49 ` redi at gcc dot gnu.org
2021-12-08 15:53 ` redi at gcc dot gnu.org
2023-12-13 20:57 ` cvs-commit at gcc dot gnu.org
2023-12-16 17:15 ` ppalka 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).