public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template
@ 2022-12-01 13:20 jlame646 at gmail dot com
2022-12-01 20:09 ` [Bug c++/107945] " pinskia at gcc dot gnu.org
` (5 more replies)
0 siblings, 6 replies; 7+ messages in thread
From: jlame646 at gmail dot com @ 2022-12-01 13:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
Bug ID: 107945
Summary: GCC accepts invalid program involving constexpr static
data member of class template
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jlame646 at gmail dot com
Target Milestone: ---
The following invalid program is accepted by gcc but rejected by clang and
msvc. https://godbolt.org/z/WGz3f75da
```
struct incomplete;
template<typename T> struct C
{
static constexpr T t{};
};
template<class T>
struct myClass {
C<T> new_t() { return {}; }
};
int main() {
myClass<incomplete> d;
d.new_t();
}
```
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] GCC accepts invalid program involving constexpr static data member of class template
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
@ 2022-12-01 20:09 ` pinskia at gcc dot gnu.org
2022-12-01 20:10 ` pinskia at gcc dot gnu.org
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-01 20:09 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced:
```
struct incomplete;
template<typename T> struct C
{
static constexpr T t{};
};
int main() {
C<incomplete> t;
// t.t;
}
```
If we uncomment t.t, then GCC will reject it.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] GCC accepts invalid program involving constexpr static data member of class template
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
2022-12-01 20:09 ` [Bug c++/107945] " pinskia at gcc dot gnu.org
@ 2022-12-01 20:10 ` pinskia at gcc dot gnu.org
2022-12-01 20:13 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized accepted pinskia at gcc dot gnu.org
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-01 20:10 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
ICC has the same behavior as GCC, though I don't know if it was EDG marking
this as an extension or not.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized accepted
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
2022-12-01 20:09 ` [Bug c++/107945] " pinskia at gcc dot gnu.org
2022-12-01 20:10 ` pinskia at gcc dot gnu.org
@ 2022-12-01 20:13 ` pinskia at gcc dot gnu.org
2024-04-02 14:26 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted jlame646 at gmail dot com
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-01 20:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|static incomplete |static constexpr incomplete
|(depedent) data member of a |(depedent) data member of a
|class template accepted |class template and
| |in-member initialized
| |accepted
Ever confirmed|0 |1
Status|UNCONFIRMED |NEW
Last reconfirmed| |2022-12-01
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
If we make the field not depedent then GCC rejects it:
```
struct incomplete;
template<typename T>
struct C
{
static constexpr incomplete t{};
};
```
Confirmed.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
` (2 preceding siblings ...)
2022-12-01 20:13 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized accepted pinskia at gcc dot gnu.org
@ 2024-04-02 14:26 ` jlame646 at gmail dot com
2024-04-03 1:28 ` bbi5291 at gmail dot com
2024-05-03 7:58 ` jlame646 at gmail dot com
5 siblings, 0 replies; 7+ messages in thread
From: jlame646 at gmail dot com @ 2024-04-02 14:26 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
--- Comment #4 from Jason Liam <jlame646 at gmail dot com> ---
Here is another invalid snippet that gcc accepts but both clang and msvc
rejects correctly. https://godbolt.org/z/sz4rczEaG
```
#include <array>
template<class T, std::size_t N>
requires std::is_arithmetic_v<T> && (N >= 1)
class Vector
{
static constexpr std::size_t Dimension = N;
std::array<T, Dimension> Elements;
public:
constexpr Vector() noexcept : Elements{} {}
constexpr ~Vector() = default;
static constexpr Vector ZeroVector{};
};
int main()
{
Vector<float, 7> boo = Vector<float, 7>::ZeroVector;
}
```
Related thread
https://stackoverflow.com/questions/78248691/static-data-member-of-template-class-type-constexpr-vs-const-constinit
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
` (3 preceding siblings ...)
2024-04-02 14:26 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted jlame646 at gmail dot com
@ 2024-04-03 1:28 ` bbi5291 at gmail dot com
2024-05-03 7:58 ` jlame646 at gmail dot com
5 siblings, 0 replies; 7+ messages in thread
From: bbi5291 at gmail dot com @ 2024-04-03 1:28 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
Brian Bi <bbi5291 at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bbi5291 at gmail dot com
--- Comment #5 from Brian Bi <bbi5291 at gmail dot com> ---
In the first case, I think GCC's behaviour can be argued to be correct.
[temp.inst]/3.1 says that when you implicitly instantiate a class template, you
only instantiate the declarations of static data members, not their
definitions. Which means that until you do something that provokes the
instantiation of the *definition* of `C<incomplete>::t`, the compiler must
treat it as a variable of type `const incomplete` that is
declared-but-not-defined, which means that you haven't yet actually attempted
to define a variable with incomplete type, which means the program is
well-formed.
In the most recent example given by Jason Liam we can reduce it to:
template <class T>
struct S {
constexpr static S s = {};
};
int main() {
auto s = S<int>::s;
}
This may be related to bug 70820, which was closed as INVALID. I think perhaps
that decision should be revisited; while I can see the argument for the above
code being well-formed, I think the interpretation of the standard being taken
in this case is wrong.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
` (4 preceding siblings ...)
2024-04-03 1:28 ` bbi5291 at gmail dot com
@ 2024-05-03 7:58 ` jlame646 at gmail dot com
5 siblings, 0 replies; 7+ messages in thread
From: jlame646 at gmail dot com @ 2024-05-03 7:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107945
--- Comment #6 from Jason Liam <jlame646 at gmail dot com> ---
Here is the thread that explains why this is ill-formed.
https://stackoverflow.com/questions/74642250/incomplete-type-works-with-gcc-but-not-with-clang-and-msvc
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-05-03 7:58 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 13:20 [Bug c++/107945] New: GCC accepts invalid program involving constexpr static data member of class template jlame646 at gmail dot com
2022-12-01 20:09 ` [Bug c++/107945] " pinskia at gcc dot gnu.org
2022-12-01 20:10 ` pinskia at gcc dot gnu.org
2022-12-01 20:13 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized accepted pinskia at gcc dot gnu.org
2024-04-02 14:26 ` [Bug c++/107945] static constexpr incomplete (depedent) data member of a class template and in-member initialized incorrectly accepted jlame646 at gmail dot com
2024-04-03 1:28 ` bbi5291 at gmail dot com
2024-05-03 7:58 ` jlame646 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).