0

I'm trying to understand this answer - typedef struct vs struct definitions [duplicate]:

(Line 3): ["] In the first line you are defining the identifier S within the struct name space (not in the C++ sense). [."]

  1. It seems like for struct X {...};, the {...} is like an adjective for X, and X {...} is thrown into something called struct-namespace, how many struct-namespace are there in a program? Is there only one specified by struct?
  2. It seems like in C/C++ struct and class are almost the same? So in C++ struct and class are both in class-namespace?
Kindred
  • 1,229
  • 14
  • 41

2 Answers2

1

From the C99 standard draft

6.2.3

6.2.3 Name spaces of identifiers

If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate namespaces for various categories of identifiers, as follows:

—label names (disambiguated by the syntax of the label declaration and use);

—the tags of structures, unions, and enumerations (disambiguated by following any of the keywords struct , union ,or enum );

—the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);

—all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

In other words, if you have struct S, then S is in the struct namespace, whereas if you have int S as well, then it's in the namespace of ordinary identifiers.

This is not the case for C++

0

1 (for C): there is one namespace for all structure tags. You can have as many different tags as you want, even if the same name is used in other namespaces.

Each structure on its own is its own namespace (there are "infinite" struct namespaces).

// You can have
struct foo { int var; }
struct bar { double var; }
struct baz { int var; }
struct qux { int var; }
// with no conflict between all the `var`
pmg
  • 106,608
  • 13
  • 126
  • 198
  • So each different `X` is a namespace? – Kindred Nov 23 '18 at 14:20
  • 1
    sort of, if you have a `struct one`, `one` is an identifier belonging to the struct namespace. `one` can also be one or more identifier(s) in other namespaces. – pmg Nov 23 '18 at 14:27
  • OK, so `struct {...} one, two, three;` only one namespaces and three identifiers for it? And is that in this case the struct is anonymous? – Kindred Nov 23 '18 at 14:30
  • still not quite right. in `struct {...} one, two, three;` there is no identifier for the struct namespace: it's a struct without a tag (an anonymous struct, using no namespace). The identifiers `one`, `two`, and `three` belong to the regular namespace. – pmg Nov 23 '18 at 14:33
  • See [this answer of mine](https://stackoverflow.com/a/7729772/25324) to an older question. – pmg Nov 23 '18 at 14:35
  • "struct tag names" in your link the same as "identifier belonging to the struct namespace" in your first comment above? – Kindred Nov 23 '18 at 14:39
  • 1
    Yes, the same meaning :) – pmg Nov 23 '18 at 14:41
  • "Each structure on its own is its own namespace", you mean those `{...}` following "different tags" are themselves namespaces? – Kindred Nov 23 '18 at 14:45
  • Yes, in the example in my answer as well as the global namespace, the label namespace, the struct/tag/enum namespace, there is the `struct foo` namespace, the `struct bar` namespace, ... Without context, you can't say what `var` is. Context is the namespace – pmg Nov 23 '18 at 14:48
  • Don't worry, I upvoted your answer, I'm reading your link and thinking about which part I'm still confused. I'll give you another one after I understand it. – Kindred Nov 23 '18 at 14:57