为何在结构体中使用typedef?VS中结构体别名的对象声明问题
typedef with Structs, and That Visual Studio Quirk Explained Great questions—let's unpack them clearly, since they touch on both C/C++ compatibility and subtle naming rules that trip up even experienced devs sometimes.
1. Why bother with typedef for structs when there's a simpler way?
The short answer: it’s mostly a holdover from C, but it still has niche uses in modern C++.
Back in C, when you defined a struct like this:
struct MyStruct { int x; };
You couldn’t just declare an instance with MyStruct obj;—you had to write struct MyStruct obj; every single time. typedef fixed this by creating a shorter, cleaner alias for the struct type:
typedef struct MyStruct { int x; } MyStruct;
Now you can write MyStruct obj; directly, which cuts down on boilerplate, especially if you use the struct frequently.
In C++, this isn’t strictly necessary at all—when you define a struct, the name is automatically added to the global namespace. So struct MyStruct { ... }; lets you do MyStruct obj; without any typedef. That said, people still use typedef with structs in C++ for a few reasons:
- Backward compatibility: If code needs to compile as both C and C++,
typedefkeeps the syntax consistent across languages. - Simplifying complex types: For template-heavy structs (like
std::map<int, std::string>), atypedef(or the newerusingalias) makes code far easier to read:using IntStringMap = std::map<int, std::string>; IntStringMap myMap; - Old habits die hard: Many developers learned C first and stick with the
typedefpattern out of familiarity, even when it’s not required.
2. Why does adding that trailing name force me to use struct when declaring objects?
Ah, this is a classic case of confusing variable declaration with type aliasing—let’s break down exactly what’s happening here.
When you write this in C++:
struct CAddition { int x, y; CAddition(int a, int b) { x = a; y = b; } int result() { return x + y; } };
You’re defining a struct type named CAddition. The compiler recognizes CAddition as a valid type, so CAddition foo; works perfectly.
But when you add the trailing CAddition:
struct CAddition { int x, y; CAddition(int a, int b) { x = a; y = b; } int result() { return x + y; } } CAddition;
You’re not creating a type alias—you’re declaring a variable named CAddition of type struct CAddition. Now the compiler has two things named CAddition: the struct type, and a variable of that type.
When you try to write CAddition foo;, the compiler prioritizes the variable name over the type name. Since you can’t use a variable as a type to declare another object, it throws an error. Adding struct (struct CAddition foo;) tells the compiler explicitly that you’re referring to the struct type, not the variable.
If you did want to create a type alias in C++ (though again, it’s unnecessary here), you’d use typedef properly:
typedef struct CAddition { int x, y; CAddition(int a, int b) { x = a; y = b; } int result() { return x + y; } } CAddition;
Or the more modern C++ using syntax:
struct CAddition { int x, y; CAddition(int a, int b) { x = a; y = b; } int result() { return x + y; } }; using CAddition = struct CAddition;
But again, in pure C++, this is redundant—you can just use the struct name directly.
内容的提问来源于stack exchange,提问作者Novice_Developer

