C++ classes: 01 侯捷C++11

1 minute read

Published:

1. 演进、环境与资源

  • Head files
    • C++ 标准库head files不带(.h),如 #include
    • 新式C head files不带(.h),如 #include
    • 旧式C head files带(.h)仍可用,如 #include
    • E.g. #include #include #include #include #include #include #include
      • 上述所有标准库内的函数均在std namespace下:
        • using namespace std;
        • std::xxxx;
      • Many of TR1 (2003) features existed in the secondary namespace of std::tr1, now are part of std namespace
  • Bibliography
    • https://isocpp.org/
    • https://www.stroustrup.com/
    • https://cplusplus.com/
    • https://en.cppreference.com/
    • https://gcc.gnu.org/
    • 《The C++ Standard Library》
    • 《C++ Primer》
    • 《The C++ Programming Language》
    • 《Effective Modern C++》
  • Start
    • Test the cpp version of a compiler

      #include <iostream>
      
      int main()
      {
          // see the supported version of cpp new features in your compiler
          std::cout << __cplusplus << std::endl;
          return 0;
      }
      
  • Subjects
    • Language
      • Variadic Templates

        #include <iostream>
        #include <bits/stdc++.h>
        
        void print() // for the boundary condition, i.e., without parameters
        {
        }
        
        template <typename T, typename... Types>
        void print(const T &firstArg, const Types&... args)
        {
            std::cout << "size of args is: " << sizeof...(args) << std::endl; // sizeof...(args) yields the number of arguments
            std::cout << firstArg << std::endl; // print first argument
            print(args...);                     // call print() for remaining arguments
        }
        
        int main()
        {
            // see the supported version of cpp new features in your compiler
            std::cout << __cplusplus << std::endl;
            print(7.5, "hello", std::bitset<32>(377), 42); // 递归调用print自己,第一个参数为firstArg,剩下的为args
            return 0;
        } * 关键字 ... 表示任意个数、任意类型的数据(包) * 方便完成recursive function call
        
      • move Semantics
      • Range-base for loop
      • Initializer list
      • Lambdas
    • Standard Library
      • type_traits
      • Unordered Container
      • forward_list
      • array
      • tuple
      • Con-currency
      • RegEx
  • Others