0

I'm just trying to learn the rules of the C++ syntax.

From what I've gathered you read variable types backwards, except the keywords are in front of what they apply to. But I'm not sure how backwards, it doesn't seem to always work.

int const * foo // foo is a const pointer to an int?
const int * foo // foo is a pointer to a const int?
const int const * foo // foo is a const pointer to a const int?
static const int ** foo // foo is a pointer to a pointer to a static const int?

Is this possible to do this:

static int thread_local * foo;  // foo is a thread_local pointer to a static int?
const int static * thread_local * const * foo;
// foo is a const pointer to a thread_local pointer to a static pointer to a const int

The last one isn't accepted by Visual Studio, but (for the sake of understanding) I'm not sure why..?

Alasdair
  • 13,348
  • 18
  • 82
  • 138
  • 3
    `int const * foo // foo is a const pointer to an int?` - no, this and the second are identical. only the syntax changes. a const pointer to mutable int is `int * const foo;` Both the first and second are mutable pointers to const int. At the risk of sounding self-gratifying, look at the breakdown in [this answer](https://stackoverflow.com/a/14566215/1322972) for pointer declarations (both single and double indirection are shown). Forget the reason for the answer, those examples are what you may find interesting. – WhozCraig Mar 26 '21 at 14:11
  • 3
    Very good read: https://mariusbancila.ro/blog/2018/11/23/join-the-east-const-revolution/ – NathanOliver Mar 26 '21 at 14:12
  • The general rule is `const` refers to the thing to its *immediate left*. With the exception to the rule when `const` is first, then it refers to the thing to its *immediate right*. – Eljay Mar 26 '21 at 14:13
  • 1
    as long as no `thread_local` (or references) are involved, i like to use this: https://cdecl.org/ – 463035818_is_not_an_ai Mar 26 '21 at 14:16
  • Thanks guys! How about thread_local though? That's why I asked this question. Is thread_local different? – Alasdair Mar 26 '21 at 14:23

0 Answers0