🐭 using 关键字的应用场景
using 关键字在 C++ 中由来已久,使用得也比较多,现代 C++ 进一步扩展了其应用范围,目前 using 可以用在以下场景中:
- 命名空间声明
- 命名空间成员声明
- 类成员声明
- 类继承构造
- 类型别名与模板别名
其中的 4,5 是现代 C++ 对 using 关键字应用范围的扩展,5 是用来替代 typedef 的。
代码示例 :
#include <filesystem>#include <iostream>template <typename T, typename U> class MagicType {public:T dark;U magic;};template <typename U>using TrueDarkMagic = MagicType<std::true_type, U>; // using template alias declarationint main(void) {// case 1: namespace declarationusing namespace std::filesystem;// case 2: namespace member declarationusing std::cout, std::endl;// case 3, 4: class member declaration, inherited constructorstruct B {B() : x_{0} {};B(int y) : B() { y_ = y; }; // use delegating constructorvoid print() const { cout << '[' << x_ << ", " << y_ << "]\n"; };void print(bool) const { cout << '{' << x_ << ", " << y_ << "}\n"; };private:int x_;int y_;};// private inheritancestruct D : private B {using B::B; // case 4: inherited constructorusing B::print; // case 3: class member declaration};D d{6};d.print(); // [0, 6]d.print(true); // {0, 6}// case 5: type alias and alias template declaration#define Dstring std::stringtypedef std::string Tstring;using Ustring = std::string;typedef void (*TFP)(int, const std::string &); // typedef type alias declarationusing UFP = void (*)(int, const std::string &); // using type alias declarationTrueDarkMagic<std::string> tdm; // using template alias declarationreturn 0;}
:::tips 注意上面的代码还用到了现代 C++ 的 委托构造与继承构造 新特性 :::
🐂 using 与 typedef
现代 C++ 扩展了 using 关键字的语义,其中就包含了对 typedef 的替换,那 C++ 在设计上为啥不直接扩展 typedef 呢?这个是为了兼容 C,谁也不能保证将来 C 会不会扩展 typedef。因此,在 C++ 编程中推荐无脑的使用 using 关键字,遇到使用 typedef 的老代码也要能看得懂,知道怎么使用 using 来改善。
#include <iostream>template <typename T, typename U> class MagicType {public:T dark;U magic;};template <typename U> struct TrueDarkMagic0 {typedef MagicType<std::true_type, U> type; // typedef};template <typename U>using TrueDarkMagic1 = typename TrueDarkMagic0<U>::type; // using with typenametemplate <typename T> using TrueDarkMagic2 = MagicType<std::true_type, T>; // using#if 0template <typename T>typedef MagicType<std::true_type, T> TrueDarkMagic3; // not compiles#endifint main(void) {using std::cout, std::is_same_v;cout.setf(std::ios_base::boolalpha);// case 1: using is more clear than typedeftypedef void (*TFP)(int, const std::string &); // typedefusing UFP = void (*)(int, const std::string &); // using// case 2: alias template declarationTrueDarkMagic0<std::string>::type tdm0;TrueDarkMagic1<std::string> tdm1;TrueDarkMagic2<std::string> tdm2;cout << is_same_v<decltype(tdm0), decltype(tdm1)> << '\n'; // truecout << is_same_v<decltype(tdm0), decltype(tdm2)> << '\n'; // true// case 3: C++ type_traits also use using prefer to typedef// template <class _Tp> using remove_const_t = typename remove_const<_Tp>::type;const int x0 = 1;const int y0 = 1;std::remove_const<decltype(x0)>::type x00 = 1;std::remove_const_t<decltype(y0)> y00 = 1;cout << is_same_v<decltype(x00), int> << '\n'; // truecout << is_same_v<decltype(x00), int> << '\n'; // truereturn 0;}
:::tips ‘using’ 用起来确实要比 ‘typedef’ 要方便,标准库也在大量使用 ‘using’ 来改善老的 ‘typedef’ 设计 :::
🐯 总结
- typedef 不能直接定义模板别名,需要定义额外的模板类来支持
- using 是更好的 typedef,推荐使用 using 来改善老的 typedef 设计
- 也要注意 using 的其它应用特性
