Ruggero Turra
Ruggero Turra

Reputation: 17740

struct vector works

I did a mistake in my program, I wrote

struct vector<int> v;

instead of

vector<int> v;

but it seems that the compiler doesn't care: http://codepad.org/TCPb8p2u

Why does it work? Is there some differencies with or without struct?

Upvotes: 2

Views: 247

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320777

The others have already explained it that what you uses is called elaborated type specifier, which can be used for name unhiding and ambiguity resolution.

However, what also works here is one curious feature of C++ language, which states that class-key (i.e. class or struct keyword) used in class declaration is not required to agree with the class-key used in the elaborated type specifier. E.g. you can declare your class with class keyword and then later refer to it with struct keyword (and vice versa). There's no error in it

class S {};

int main() {
  struct S s; // OK, 's' has type `S` (i.e. `class S`)
}

Standard std::vector template class is declared with keyword class, but there's no error in referring to it as struct std::vector, which is what you did in your example.

And no, it makes no difference whether in your declaration of v you use class, struct or nothing at all.

Upvotes: 2

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

If you write class, it would also work.

class vector<int> v;

See this: http://www.ideone.com/EoJxk

This is actually old C style. The C++ Standard calls it elaborated-type-specifier in section §3.4.4.

The keyword struct (or class, enum) is sometimes used to remove ambiguities, or to make hidden names visible to the compiler. Consider the following example from the Standard itself (from section §9.1/2). Please notice that there exists a struct with name stat and with exactly same also exists a function:

struct stat {
    // ...
};
stat gstat; // use plain stat to define variable

int stat(struct stat*); // redeclare stat as function

void f()
{
    struct stat* ps; // struct prefix needed to name struct stat
    // ...
    stat(ps); //call stat()
    // ...
}

§9.1/2 says,

A class definition introduces the class name into the scope where it is defined and hides any class, object, function, or other declaration of that name in an enclosing scope (3.3). If a class name is declared in a scope where an object, function, or enumerator of the same name is also declared, then when both declarations are in scope, the class can be referred to only using an elaborated-type-specifier (3.4.4).

Upvotes: 6

Collin Dauphinee
Collin Dauphinee

Reputation: 14003

This is a feature of C++ that's used to resolve ambiguity between a variable and type with the same name. I believe they're called 'elaborate type specifiers.'

You can use a keyword to tell the compiler exactly what you mean, when there would normally be ambiguity.

Take this for example:

int x = 0;
class x { };

// Compiler error! Am I refering to the variable or class x?
x y;

// This is okay, I'm telling the compiler which x I'm referring to.
class x y;

This can also be used to specify enums and unions, not just structs and classes, though you can only have one user-defined type with the same name.

Upvotes: 2

Related Questions