🐭 using 关键字的应用场景

using 关键字在 C++ 中由来已久,使用得也比较多,现代 C++ 进一步扩展了其应用范围,目前 using 可以用在以下场景中:

  1. 命名空间声明
  2. 命名空间成员声明
  3. 类成员声明
  4. 类继承构造
  5. 类型别名与模板别名

其中的 4,5 是现代 C++ 对 using 关键字应用范围的扩展,5 是用来替代 typedef 的。
代码示例 :

  1. #include <filesystem>
  2. #include <iostream>
  3. template <typename T, typename U> class MagicType {
  4. public:
  5. T dark;
  6. U magic;
  7. };
  8. template <typename U>
  9. using TrueDarkMagic = MagicType<std::true_type, U>; // using template alias declaration
  10. int main(void) {
  11. // case 1: namespace declaration
  12. using namespace std::filesystem;
  13. // case 2: namespace member declaration
  14. using std::cout, std::endl;
  15. // case 3, 4: class member declaration, inherited constructor
  16. struct B {
  17. B() : x_{0} {};
  18. B(int y) : B() { y_ = y; }; // use delegating constructor
  19. void print() const { cout << '[' << x_ << ", " << y_ << "]\n"; };
  20. void print(bool) const { cout << '{' << x_ << ", " << y_ << "}\n"; };
  21. private:
  22. int x_;
  23. int y_;
  24. };
  25. // private inheritance
  26. struct D : private B {
  27. using B::B; // case 4: inherited constructor
  28. using B::print; // case 3: class member declaration
  29. };
  30. D d{6};
  31. d.print(); // [0, 6]
  32. d.print(true); // {0, 6}
  33. // case 5: type alias and alias template declaration
  34. #define Dstring std::string
  35. typedef std::string Tstring;
  36. using Ustring = std::string;
  37. typedef void (*TFP)(int, const std::string &); // typedef type alias declaration
  38. using UFP = void (*)(int, const std::string &); // using type alias declaration
  39. TrueDarkMagic<std::string> tdm; // using template alias declaration
  40. return 0;
  41. }

:::tips 注意上面的代码还用到了现代 C++ 的 委托构造与继承构造 新特性 :::

🐂 using 与 typedef

现代 C++ 扩展了 using 关键字的语义,其中就包含了对 typedef 的替换,那 C++ 在设计上为啥不直接扩展 typedef 呢?这个是为了兼容 C,谁也不能保证将来 C 会不会扩展 typedef。因此,在 C++ 编程中推荐无脑的使用 using 关键字,遇到使用 typedef 的老代码也要能看得懂,知道怎么使用 using 来改善。

  1. #include <iostream>
  2. template <typename T, typename U> class MagicType {
  3. public:
  4. T dark;
  5. U magic;
  6. };
  7. template <typename U> struct TrueDarkMagic0 {
  8. typedef MagicType<std::true_type, U> type; // typedef
  9. };
  10. template <typename U>
  11. using TrueDarkMagic1 = typename TrueDarkMagic0<U>::type; // using with typename
  12. template <typename T> using TrueDarkMagic2 = MagicType<std::true_type, T>; // using
  13. #if 0
  14. template <typename T>
  15. typedef MagicType<std::true_type, T> TrueDarkMagic3; // not compiles
  16. #endif
  17. int main(void) {
  18. using std::cout, std::is_same_v;
  19. cout.setf(std::ios_base::boolalpha);
  20. // case 1: using is more clear than typedef
  21. typedef void (*TFP)(int, const std::string &); // typedef
  22. using UFP = void (*)(int, const std::string &); // using
  23. // case 2: alias template declaration
  24. TrueDarkMagic0<std::string>::type tdm0;
  25. TrueDarkMagic1<std::string> tdm1;
  26. TrueDarkMagic2<std::string> tdm2;
  27. cout << is_same_v<decltype(tdm0), decltype(tdm1)> << '\n'; // true
  28. cout << is_same_v<decltype(tdm0), decltype(tdm2)> << '\n'; // true
  29. // case 3: C++ type_traits also use using prefer to typedef
  30. // template <class _Tp> using remove_const_t = typename remove_const<_Tp>::type;
  31. const int x0 = 1;
  32. const int y0 = 1;
  33. std::remove_const<decltype(x0)>::type x00 = 1;
  34. std::remove_const_t<decltype(y0)> y00 = 1;
  35. cout << is_same_v<decltype(x00), int> << '\n'; // true
  36. cout << is_same_v<decltype(x00), int> << '\n'; // true
  37. return 0;
  38. }

:::tips ‘using’ 用起来确实要比 ‘typedef’ 要方便,标准库也在大量使用 ‘using’ 来改善老的 ‘typedef’ 设计 :::

🐯 总结

  1. typedef 不能直接定义模板别名,需要定义额外的模板类来支持
  2. using 是更好的 typedef,推荐使用 using 来改善老的 typedef 设计
  3. 也要注意 using 的其它应用特性