public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers)
@ 2022-12-19  4:41 danakj at orodu dot net
  2022-12-19  4:47 ` [Bug c++/108169] class type template parameters are const " danakj at orodu dot net
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: danakj at orodu dot net @ 2022-12-19  4:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108169
           Summary: class type template parameters are non-const in GCC
                    (differs from other compilers)
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: danakj at orodu dot net
  Target Milestone: ---

Reproduces on GCC 10, 11, 12 and Trunk.

An auto template parameter's type is const in Clang and MSVC, but is considered
non-const in GCC. This does not occur with primitive types, but it does with
class types.

This is a C++20 bug, as C++20 introduced class type template parameters. With
-std=c++17, gcc states as such:
```
<source>:15:26: error: non-type template parameters of class type only
available with '-std=c++20' or '-std=gnu++20'
```

Source code:
```
#include <type_traits>

template <auto V>
struct S {
    static constexpr bool is_const = std::is_const_v<decltype(V)>;
};

struct W {
    constexpr W(int i) : i(i) {}
    int i;
};

int main() {
    static_assert(!S<1>::is_const);  // pass
    static_assert(!S<W(2)>::is_const);  // fail
}
```

Godbolt: https://godbolt.org/z/WEbPcjzcK

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
@ 2022-12-19  4:47 ` danakj at orodu dot net
  2022-12-19  7:52 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: danakj at orodu dot net @ 2022-12-19  4:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from danakj at orodu dot net ---
Sorry I inverted my logic.

In Clang and MSVC an `auto` class-type template parameter is not const. In GCC
it is.

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
  2022-12-19  4:47 ` [Bug c++/108169] class type template parameters are const " danakj at orodu dot net
@ 2022-12-19  7:52 ` pinskia at gcc dot gnu.org
  2022-12-26  3:23 ` mail at jhellings dot nl
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-19  7:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, take:
```
struct W {
    constexpr W(int i) : i(i) {}
    int i;
};

template <auto V>
struct S {
    static constexpr decltype(V) &t = V;
};
int main() {
    const W *t = &S<W(2)>::t;
}
```
Clang produces:
```
<source>:7:35: error: binding reference of type 'decltype(W{2})' (aka 'W') to
value of type 'const W' drops 'const' qualifier
    static constexpr decltype(V) &t = V;
                                  ^   ~
<source>:10:28: note: in instantiation of static data member 'S<W{2}>::t'
requested here
    const W *t = &S<W(2)>::t;
                           ^
```
So clang thinks V is const too in some cases ....

Even MSVC produces:
```
<source>(7): error C2440: 'initializing': cannot convert from 'const W' to 'W
&'
<source>(7): note: Conversion loses qualifiers
<source>(10): note: see reference to class template instantiation 'S<W{int:2}>'
being compiled
```
Notice the `const W` there.

Note for scalar types (though float is broken for clang because it is not
implemented yet), it is always non-const ...

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
  2022-12-19  4:47 ` [Bug c++/108169] class type template parameters are const " danakj at orodu dot net
  2022-12-19  7:52 ` pinskia at gcc dot gnu.org
@ 2022-12-26  3:23 ` mail at jhellings dot nl
  2022-12-26  4:00 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mail at jhellings dot nl @ 2022-12-26  3:23 UTC (permalink / raw)
  To: gcc-bugs

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

mail at jhellings dot nl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mail at jhellings dot nl

--- Comment #3 from mail at jhellings dot nl ---
I looked a bit further into this and into what the standard says. GCC seems to
derive the wrong type for non-type template parameters in a few cases both (1)
when they have a placeholder type such as auto and (2) when they use a normal
type. See https://jhellings.nl/article?articleid=1 for the full analysis.

My findings (summary):
Let V be a non-type template parameter. The C++20 standard defines decltype(V)
as follows (Clause 1 of Section [dcl.type.decltype]):

"For an expression E, the type denoted by decltype(E) is defined as follows:
... otherwise, if E is an unparenthesized id-expression naming a non-type
template-parameter, decltype(E) is the type of the template-parameter after
performing any necessary type deduction;"

In the above, the type of a template-parameter is defined as follows (Clause 6
of Section [temp.param]):

"A non-type template-parameter shall have one of the following (possibly
cv-qualified) types: ... The top-level cv-qualifiers on the template-parameter
are ignored when determining its type."

Hence, decltype(V) with V a non-type template parameter should never yield a
type with a top-level const qualifier.

In GCC 12 (x86-64 gcc 12.2 on Compiler Explorer), GCC does not follow this rule
in the following case: if the type of V is a structural class type T, then GCC
always yields a ``const T''.


See, for example, the following program _that does not use placeholder types_:

/*
 * @author{Jelle Hellings}.
 */
#include <iostream>
#include <typeinfo>
#include <type_traits>


/*
 * Format the type @{Type} and print it to standard output. The qualifiers
 * @{qs...} are used to include further details on @{Type}.
 */
template<class Type>
void type_printer(const auto&... qs)
{
    if constexpr (std::is_lvalue_reference_v<Type>) {
        return type_printer<std::remove_reference_t<Type>>(qs..., "&");
    }
    if constexpr (std::is_rvalue_reference_v<Type>) {
        return type_printer<std::remove_reference_t<Type>>(qs..., "&&");
    }
    if constexpr (std::is_const_v<Type>) {
        return type_printer<std::remove_const_t<Type>>(qs..., "const");
    }
    if constexpr (std::is_volatile_v<Type>) {
        return type_printer<std::remove_volatile_t<Type>>(qs..., "volatile");
    }
    if constexpr (std::is_array_v<Type>) {
        return type_printer<std::remove_extent_t<Type>>(qs..., "[]");
    }
    if constexpr (std::is_pointer_v<Type>) {
        return type_printer<std::remove_pointer_t<Type>>(qs..., "*");
    }
    ((std::cout << qs << " "), ...);
    std::cout << '`' << typeid(Type).name() << '`' << std::endl;
}


/*
 * Print the details on the type @{Type}, the type of a non-type template
 * parameter with value @{Value}, and on the type of expressions involving
 * @{Value}. The printed data will be labeled with @{case_name}.
 */
template<class Type, Type Value = Type{}>
void inspect_template_argument(const auto& case_name)
{
    type_printer<Type>(case_name);
    type_printer<decltype(Value)>(case_name);
}


/*
 * A dummy struct type.
 */
struct dummy {};


/*
 * Entry-point of the program.
 */
int main()
{
    /* We look at most combinations of types and values. */
    inspect_template_argument<int>("{int}");
    inspect_template_argument<const int>("{c-int}");
    inspect_template_argument<dummy>("{dummy}");
    inspect_template_argument<const dummy>("{c-dummy}");
}


The above program SHOULD return:

{int} `int`
{int} `int`
{c-int} const `int`
{c-int} `int`
{dummy} `5dummy`
{dummy} `5dummy`
{c-dummy} const `5dummy`
{c-dummy} `5dummy`

But, in x86-64 gcc 12.2 (on Compiler Explorer), it returns

{int} `i`
{int} `i`
{c-int} const `i`
{c-int} `i`
{dummy} `5dummy`
{dummy} const `5dummy`     << WRONG to the best of my understanding
{c-dummy} const `5dummy`
{c-dummy} const `5dummy`   << WRONG to the best of my understanding

Note that in x86-64 gcc 11.3 (on Compiler Explorer), the output was more-wrong,
as also the const-qualifier for non-class types was preserved in some cases.
Indeed, x86-64 gcc 11.3 (on Compiler Explorer) returns:

{int} `i`
{int} `i`
{c-int} const `i`
{c-int} const `i`         << WRONG & FIXED to the best of my understanding
{dummy} `5dummy`
{dummy} const `5dummy`    << WRONG to the best of my understanding
{c-dummy} const `5dummy`
{c-dummy} const `5dummy`  << WRONG to the best of my understanding



The issues detected above carry over to the usage of placeholder types, leading
to the bug reported in this bug report.

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (2 preceding siblings ...)
  2022-12-26  3:23 ` mail at jhellings dot nl
@ 2022-12-26  4:00 ` pinskia at gcc dot gnu.org
  2022-12-26  4:01 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-26  4:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to mail from comment #3)> 
> Note that in x86-64 gcc 11.3 (on Compiler Explorer), the output was
> more-wrong, as also the const-qualifier for non-class types was preserved in
> some cases. Indeed, x86-64 gcc 11.3 (on Compiler Explorer) returns:

So that was PR 100893 .

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (3 preceding siblings ...)
  2022-12-26  4:00 ` pinskia at gcc dot gnu.org
@ 2022-12-26  4:01 ` pinskia at gcc dot gnu.org
  2022-12-26  4:24 ` mail at jhellings dot nl
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-26  4:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-12-26
     Ever confirmed|0                           |1

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So confirmed.

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (4 preceding siblings ...)
  2022-12-26  4:01 ` pinskia at gcc dot gnu.org
@ 2022-12-26  4:24 ` mail at jhellings dot nl
  2022-12-26  4:27 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mail at jhellings dot nl @ 2022-12-26  4:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from mail at jhellings dot nl ---
Dear Andrew,

My excuses for ruining your Christmas ??!

Thanks for confirming the bug so quickly, I hope my analysis helps solving this
and other cases.

With kind regards,
Jelle Hellings

-----Original Message-----
From: pinskia at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org>
Sent: Sunday, December 25, 2022 11:02 PM
To: mail@jhellings.nl
Subject: [Bug c++/108169] class type template parameters are const in GCC
(differs from other compilers)

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

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

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

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (5 preceding siblings ...)
  2022-12-26  4:24 ` mail at jhellings dot nl
@ 2022-12-26  4:27 ` pinskia at gcc dot gnu.org
  2022-12-26 10:03 ` johelegp at gmail dot com
  2022-12-26 17:10 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-26  4:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to mail from comment #6)
> Dear Andrew,
> 
> My excuses for ruining your Christmas ??!


You didn't ruin anything really. I have fun reading bugzilla reports really.

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (6 preceding siblings ...)
  2022-12-26  4:27 ` pinskia at gcc dot gnu.org
@ 2022-12-26 10:03 ` johelegp at gmail dot com
  2022-12-26 17:10 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: johelegp at gmail dot com @ 2022-12-26 10:03 UTC (permalink / raw)
  To: gcc-bugs

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

Johel Ernesto Guerrero Peña <johelegp at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johelegp at gmail dot com

--- Comment #8 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
This seems to be the same as Bug 99631.

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

* [Bug c++/108169] class type template parameters are const in GCC (differs from other compilers)
  2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
                   ` (7 preceding siblings ...)
  2022-12-26 10:03 ` johelegp at gmail dot com
@ 2022-12-26 17:10 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-26 17:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Yes it is a dup of bug 99631.

*** This bug has been marked as a duplicate of bug 99631 ***

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

end of thread, other threads:[~2022-12-26 17:10 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-19  4:41 [Bug c++/108169] New: class type template parameters are non-const in GCC (differs from other compilers) danakj at orodu dot net
2022-12-19  4:47 ` [Bug c++/108169] class type template parameters are const " danakj at orodu dot net
2022-12-19  7:52 ` pinskia at gcc dot gnu.org
2022-12-26  3:23 ` mail at jhellings dot nl
2022-12-26  4:00 ` pinskia at gcc dot gnu.org
2022-12-26  4:01 ` pinskia at gcc dot gnu.org
2022-12-26  4:24 ` mail at jhellings dot nl
2022-12-26  4:27 ` pinskia at gcc dot gnu.org
2022-12-26 10:03 ` johelegp at gmail dot com
2022-12-26 17:10 ` 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).