views:

366

answers:

7

Hi, I am just trying to check whether compiler allows type name as variable name. When i tried

int int;

It reported an error saying

error C2632: 'int' followed by 'int' is illegal

But when i tried

#include <string>
using namespace std;

int main()
{
    string string;
}

It didn't give any error. Both string and int are data types.

Why compiler allows string and doesn't allow int ?

EDIT: includes updated.

EDIT: Some people are saying that int is not a class. In that case, why below line is allowed.

int a(10);

it works similar to constructor of a class.

+21  A: 

string is not a C++ reserved word, but int is, and a reserved word cannot be used as an identifier.

And its syntactically fine to have class name and object name to be same.

class test {}; 
int main() { 
        test test; // test is an object of type test.
} 
codaddict
Note: Perfectly fine, but any maintainer is going to **kill** you if you do this in real code ;) +1
Billy ONeal
This answer is correct, but I take issue with "its fine to have class name and object name to be same". The Standard may allow it, but it is very much not "fine."
John Dibling
@Bill and John: Agree.
codaddict
It's even allowed to say `class test {}; test test;` in C++.
Johannes Schaub - litb
I disagree John. In most cases, yes, I would avoid it. But how about Color color? Stuff like that makes sense sometimes, nothing is absolute.
Ed Swangren
"class test {} test;" is also allowed
Markus Kull
Even worse: http://stackoverflow.com/questions/1995113/strangest-language-feature/2000491#2000491
KennyTM
@Ed: `Color color;` is fine, since the cases are different. There is no excuse for `Color Color;` though. If you _must_ use the same name as the class, just use static variables/constants, i.e., `Color::BLUE`.
Chinmay Kanchi
+3  A: 

int is a keyword, whereas string is the name of a class in the standard library but is not a keyword.

Justice
+1  A: 

string isn't actually a "data type" in the same sense the int is. int is a "native type" and as such, the text "int" is a keyword.

string is just a "user-defined" class (although, here the "user" the defined it is the C++ standards committtee). But as such, "string" is not a keyword.

So int int is two keywords together, but string string is just defining a varaible named "string" of type "string". The C++ compiler can keeps the separate two uses of "string" straight (but it's not a good idea to do this, since, as you've demonstrated, programmers often can't).

James Curran
+6  A: 

int is a C++ keyword. In the second declaration 'string string' declares an object of type 'std::string'. After this the name 'string' hides 'std::string' in an unqualified lookup

#include <string>
using std::string;

int main(){
    string string;

    string s;  // Error
}
Chubsdad
Note that the the first use (`string string`) is only valid if `<string>` is `#include` 'd and `using namespace std;` is present in the file.
Billy ONeal
@BillyONeal: Or `using std::string` :)
ereOn
@ereOn: Good point :)
Billy ONeal
:). I just pasted the significant code.
Chubsdad
+1 for letting me know about other error.
bjskishore123
A: 

Well, given that others have essentially posted the answer, I'm going to go ahead and post what I meant...

I'm assuming that because the second answer compiles, that you have using namespace std; in your file (which is in general not a good idea; I fail to see why people tell beginning C++ users to do this).

When the compiler goes to resolve the first string, it is able to find the class in namespace std. The second use of string is simply the name of the variable.

Billy ONeal
A: 

The compiler allows primitive types to behave like classes for the purposes of templates - if you had to specialize for primitives everywhere it would be a nightmare.

DeadMG
A: 

Try removing

#include <string>