C++ 20 Pointer Declaration

GCC Still generating error messages despite the synate is correct for the pointer declaration:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
      int value = 12;
      const int *  [[no_unique_address]] volatile pointer = &value;
      cout <<" Memory Address :" << pointer;  
      return 0;
}


Error Message by GCC Compiler.

18:25:32 **** Build of configuration Debug for project LessionNo.3 ****
make all
Building file: ../Naive Bayes classifier.cpp
Invoking: GCC C++ Compiler
g++ -std=c++2a -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Naive Bayes classifier.d" -MT"Naive Bayes classifier.o" -o "Naive Bayes classifier.o" "../Naive Bayes classifier.cpp"
../Naive Bayes classifier.cpp: In function ‘int main()’:
../Naive Bayes classifier.cpp:16:42: error: expected unqualified-id before ‘volatile’
16 | const int * [[no_unique_address]] volatile pointer = &value;
| ^~~~~~~~
../Naive Bayes classifier.cpp:18:37: error: ‘pointer’ was not declared in this scope
18 | cout <<" Memory Address :" << pointer;
| ^~~~~~~
../Naive Bayes classifier.cpp:14:8: warning: unused variable ‘value’ [-Wunused-variable]
14 | int value = 12;
| ^~~~~
make: *** [subdir.mk:20: Naive Bayes classifier.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

18:25:33 Build Failed. 3 errors, 1 warnings. (took 476ms)
Last edited on
What attributes are you applying in your pointer declaration with [[attributes_list]]?

I can't find any reference that [[attributes_list]] is a valid attribute. It is just a generic placeholder with no meaning, so the compiler gets confused.
And....
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Using output tags is also recommended...
1
2
const int *  [[any_standard_C++_Attributes]] volatile pointer = &value;  
 // Generate errors. despite synatx is correct : Is there specific location for attributes in the delaration ?  
Last edited on
1
2
3
4
5
6
7
8
9
10
int main()
{
      int value = 12;

      [[maybe_unused]] const int* volatile pointer_0 = &value ; // this is fine

      // *** error *** : attribute [[no_unique_address]] can only be applied to
      //                 non-bit-field non-static data members of a class type
      [[no_unique_address]] const int* volatile pointer_1 = &value;
}
Pointer declaration
C++ C++ language Declarations

Syntax
A pointer declaration is any simple declaration whose declarator has the form

* attr(optional) cv(optional) declarator (1)
Is there is a specific location for attr(optional) in the pointer declaration, does it come before or after the cv(optional)?
Last edited on
In declarations, attributes may appear both before the whole declaration and directly after the name of the entity that is declared, in which case they are combined. In most other situations, attributes apply to the directly preceding entity.
https://en.cppreference.com/w/cpp/language/attributes#Explanation


1
2
3
4
5
6
7
8
9
10
11
int main()
{
      int value = 12;

      [[maybe_unused]] const int* volatile pointer_0 = &value ; // this is fine

      const int* volatile pointer_1 [[maybe_unused]] = &value ; // this too is fine
      
      // *error *** : the attribute [[maybe_unused]] can't be applied to types
      const int* [[maybe_unused]] volatile pointer_2 = &value ; // this is fine
}
Last edited on
The last declaration :

1
2
          const int* [[maybe_unused]] volatile pointer_2 = &value ; // this is fine
                         


still generating errors.
It is an error: // *error *** : the attribute [[maybe_unused]] can't be applied to types

This attribute can be applied to only one of the entities specified here:
https://en.cppreference.com/w/cpp/language/attributes/maybe_unused#Explanation
Last edited on
So use one of the other two.

g++ does complain about the placement of volatile in the third example. But JLBorges did have line 9 mentioning an error, perhaps shouldn't have had "this is fine" on line 10, maybe a copy / paste typo.

Also compile with -std=c++20 rather than c++2a. The g++ manual does not seem to mention using c++2a, but explicitly says use c++20, in the 11.2 manual:

gccmanual wrote:
Another revised ISO C++ standard was published in 2020 as ISO/IEC 14882:2020, and is referred to as C++20; before its publication it was sometimes referred to as C++2a. GCC supports most of the changes in the new specification. For further details see https://gcc.gnu.org/projects/cxx-status.html#cxx20. To select this standard in GCC, use the option -std=c++20.


I wonder about the benefits of always compiling against the latest standard c++23, whether it will be subtlety different because of bug fixes or other improvements in the compiler? This is despite there being no c++23 code in the file. Perhaps the improvements are related to the compiler version not the standard?
Last edited on
When I change the position of the attribute, it doesn't generate erros:

i.e

1
2
3
                 
const int*  volatile [[maybe_unused]] pointer_2 = &value ; // No errors.
 
Last edited on
Weirdly enough this works
const int [[maybe_unused]] * volatile pointer_2 = &value ; // this is fine

At least no errors rear up.
When I change the position of the attribute

Same position of the attribute that JLBorges had for pointer_0. Were you expecting it wouldn't work when creating pointer_2?
maybe the volatile and * need to be together.
TheIdeasMan:


I am using GCC 11 Compiler C++ 20 standard.
Last edited on
> shouldn't have had "this is fine" on line 10, maybe a copy / paste typo.

Yes. Correcting it now.
It is Fine now.!

The issue is the compiler! It is bug. When I run the code with Clang it works fine .
Last edited on
I using GCC 11 Compiler C++ 20 standard.


Note that c++2a (which you had in your OP) is the experimental version prior to the actual c++20 standard. c++20 conforms to the actual standard.

Did you mean g++ 11.1 or g++ 11.2 ?
When I run the code with Clang it works fine


Really? Still an error using clang 13.0.0

JLBorges gave you the reason why it is an error.
The definitive answer is in the language grammar:
https://eel.is/c++draft/dcl.decl.general#5

I'm not exactly sure what program OP is testing, but this correct program:
1
2
3
4
int main()
{
  int* [[my::attribute]] const p = nullptr;
}

Is rejected by GCC 11, but accepted by Clang 5. This appears to be a compiler bug in GCC, unless I am interpreting the grammar incorrectly.

Some time ago I was told there were problems in the standard, where attributes were incorrectly allowed or disallowed within certain grammatical productions, against the intent of the design. This is a vague memory, so I don't know to what extent the claim is or was true, but it could partially explain the implementation divergence.
Last edited on
Topic archived. No new replies allowed.