C++ using
关键字总结(对比 typedef
)
1. using
与 typedef
的区别
using
是 C++11 引入的关键字,可用于类型别名、函数指针、模板别名和继承成员,相比 typedef
更清晰、功能更强大。
特性 | typedef | using (推荐) |
---|---|---|
基本类型别名 | ✅支持 | ✅支持(更简洁) |
函数指针别名 | ✅支持 | ✅支持(更清晰) |
指针类型别名 | ✅支持 | ✅支持(更直观) |
结构体别名 | ✅支持 | ✅支持(更简洁) |
STL 容器别名 | ✅支持 | ✅支持(更易读) |
模板类型别名 | ❌不支持 | ✅支持(C++11+ 推荐) |
继承成员 | ❌不支持 | ✅支持 |
代码可读性 | 复杂,嵌套写法多 | 更清晰直观 |
2. 基本类型别名
// typedef 方式
typedef unsigned int UInt1;
// using 方式(推荐)
using UInt2 = unsigned int;
int main() {
UInt1 a = 10;
UInt2 b = 20;
return 0;
}
✅ using
语法更直观。
3. 函数指针类型别名
#include <iostream>
// typedef 方式
typedef void (*FunctionType1)(double);
// using 方式(推荐)
using FunctionType2 = void (*)(double);
void exampleFunc(double x) {
std::cout << "Function called with " << x << std::endl;
}
int main() {
FunctionType1 f1 = exampleFunc;
FunctionType2 f2 = exampleFunc;
f1(3.14);
f2(2.71);
return 0;
}
✅ using
语法更清晰,避免 typedef
复杂的写法。
4. 指针类型别名
// typedef 方式
typedef int* IntPtr1;
// using 方式(推荐)
using IntPtr2 = int*;
int main() {
int x = 5;
IntPtr1 p1 = &x;
IntPtr2 p2 = &x;
return 0;
}
✅ using
让代码更直观。
5. 结构体类型别名
#include <iostream>
// typedef 方式
typedef struct {
int x;
int y;
} Point1;
// using 方式(推荐)
using Point2 = struct {
int x;
int y;
};
int main() {
Point1 p1 = {1, 2};
Point2 p2 = {3, 4};
std::cout << "p1: " << p1.x << ", " << p1.y << std::endl;
std::cout << "p2: " << p2.x << ", " << p2.y << std::endl;
return 0;
}
✅ using
更简洁,不需要 ::type
。
6. STL 容器别名
#include <vector>
// typedef 方式
typedef std::vector<int> IntVector1;
// using 方式(推荐)
using IntVector2 = std::vector<int>;
int main() {
IntVector1 v1 = {1, 2, 3};
IntVector2 v2 = {4, 5, 6};
return 0;
}
✅ using
让代码更整洁。
7. 模板类型别名
C++03 不能为模板定义别名,而 using
可以:
#include <vector>
// C++03(不支持)
/*
template <typename T>
typedef std::vector<T> Vec; // 错误
*/
// C++11 using 方式
template <typename T>
using Vec = std::vector<T>;
Vec<int> v; // 等价于 std::vector<int>
// typedef 方式
template<typename T>
struct str_map {
typedef std::map<std::string, T> type;
};
str_map<int>::type map1; // 等价于 std::map<std::string, int>
// using 方式(推荐)
template<typename T>
using str_map_t = std::map<std::string, T>;
str_map_t<int> map2; // 等价于 std::map<std::string, int>
// typedef 方式
template<typename T>
struct func_t_def {
typedef void (*type)(T, T);
};
func_t_def<int>::type func1; // 等价于 void(*)(int, int)
// using 方式(推荐)
template<typename T>
using func_t_using = void(*)(T, T);
func_t_using<int> func2; // 等价于 void(*)(int, int)
template <typename First, typename Second, int Third>
class SomeType {};
// typedef(C++03,不支持)
/*
template <typename Second>
typedef SomeType<int, Second, 5> AliasType; // 错误
*/
// using(C++11+,推荐)
template <typename Second>
using SomeAlias = SomeType<int, Second, 5>;
SomeAlias<double> obj; // 等价于 SomeType<int, double, 5>
✅ using
支持模板别名,typedef
无法做到。
8. using
继承基类成员
#include <iostream>
class Base {
public:
void show() { std::cout << "Base::show()" << std::endl; }
};
// 继承基类的 show() 方法
class Derived : public Base {
public:
using Base::show; // 继承 Base::show
};
int main() {
Derived d;
d.show(); // 调用 Base::show()
return 0;
}
✅ using
让继承更方便。
总结
用法 | typedef | using (推荐) |
---|---|---|
基本类型别名 | ✅支持 | ✅支持(更简洁) |
模板类型别名 | ❌不支持 | ✅支持(C++11+) |
容器类型别名 | ✅支持 | ✅支持(更清晰) |
函数指针类型 | ✅支持 | ✅支持(更易读) |
继承成员 | ❌不支持 | ✅支持 |
可读性 | 复杂,嵌套写法多 | 更清晰直观 |
最终结论
using
语法更直观,功能更强,推荐用于所有类型和模板别名。typedef
仍然有效,但在现代 C++(C++11+)应优先使用using
。