Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can auto be used for static class variable definition?

Consider this simple example:

struct Class {
    struct Inner {};
    static Inner a;
};

I cannot define this a with this syntax:

auto Class::a = Inner{}; - gcc and msvc both complaint in a similar ways:

<source>:9:6: error: conflicting declaration 'auto Class::a'
    9 | auto Class::a = Inner{};
      |      ^~~~~
<source>:6:18: note: previous declaration as 'Class::Inner Class::a'
    6 |     static Inner a;
      |                  ^

but clang accepts this code.

Of course the reason for this syntax is to not repeat Class:: twice:

Class::Inner Class::a{};

So, the question - is it correct (clang is right) or incorrect (gcc and msvc are right)?

like image 921
PiotrNycz Avatar asked Oct 30 '25 15:10

PiotrNycz


1 Answers

It's core issue #2389. Clang is right, the code is valid.

2389. Agreement of deduced and explicitly-specified variable types

Section: 9.2.9.7 [dcl.spec.auto]   Status: CD6   Submitter: Nina Ranns   Date: 2018-10-24

The Standard does not explicitly address whether an example like the following is well-formed or not:

struct S {
  static int i;
};
auto S::i = 23;

There is implementation divergence on the handling of this example.

Notes from the July, 2019 meeting

Editorially add the example as well-formed; see editorial issue 2979.

CWG 2019-09-16

Approved editorial change 3227.

It has already been applied to C++20 as can be seen in [dcl.spec.auto.general]/p13.

Being a CD6 status (CD6: A DR/DRWP or Accepted/WP issue not resolved in C++20 but included in the Committee Draft advanced for balloting at the July, 2022 WG21 meeting), it appears that GCC hasn't implemented it yet.

like image 73
rustyx Avatar answered Nov 01 '25 06:11

rustyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!