public:it:cplusplus

C++

Useful C/C++ related websites.

Useful C/C++ related weblogs.


  • 凡是表达“只读”语义的场景都使用 const,表达“常量”语义的场景都使用 constexpr;
  • 成员函数名后的修饰词,除了const,volitile, c++ 11之后新增了 &, &&, override, final, noexcept,其中 &&&称为引用限定符,限制调用该成员函数的this是左值还是右值,详见 c++ primer 5
  • c++ 11 新增 std::ref, std::shared_ptr, std::weak_ptr, std::unique_ptr, std::function, std::tuple, std::unordered_set, std::chrono;
    • std::array: 对c数组的包装,方便stl进行数组调用,理论性能与c数组差不多。
    • std::unordered_map: 采用哈希,原有的 std::map 是红黑树。
    • 线程相关 std::thread, std::async, std::future, std::promise, std::packaged_task, std::mutex, std::condition_variable, std::call_once
    • 正则 std::regex
    • 随机数相关 <random>
    • 关于 shared_ptr 的线程安全:shared_ptr_thread_safety
  • c++ 14 新增 std::make_unique, std::shared_timed_mutex, std::shared_lock, std::integer_sequence, std::exchange, std::quoted
  • c++ 17 新增 std::variant, std::optional, std::any, std::apply, std::string_view, std::shared_mutex
    • 新增文件操作相关 std::filesystem
  • c++ 20 新增 std::format, 基于python格式化规范
    • 可中断线程 std::jthread, std::stop_toke
    • 原子引用 std::atomic_ref
    • 数学常数 std::numbers
    • 代码位置 std::source_location

› › condition_variable

  • 更改条件变量的线程需要:
    • 获取一个 std::mutex (比如通过 std::lock_guard)
    • 在锁定期间修改条件变量(即使条件变量是 atomic 类型)
    • 执行 notify_one 或 notify_all on the std::condition_variable,此时锁可先释放。
  • 等待条件变量的线程需要:
    • 获取 std::unique_lock<std::mutex>, 与其它更改条件变量的是同一把锁。
    • 使用预重载的带pred参数的 wait, wait_for, and wait_until, 这些已包含通常的三个步骤:
      • 检查条件变量
      • 执行不带 pred 参数的 wait 来释放锁以及挂起线程
      • 条件变量通知、或者超时、或者线程被虚假唤醒后,会自动获得锁,此时应检查条件变量,如果仍然为否则继续执行wait等待

› › Tips

  • 关于Lambda函数的存储 lambda相当于保存在堆上的函数对象
  • 以下拷贝于 C++ 11 Lambda表达式
  • C++ 11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作。Lambda的语法形式如下:
    [函数对象参数] (操作符重载函数参数) mutable或exception声明 ->返回值类型 {函数体}

    可以看到,Lambda主要分为五个部分,下面分别进行介绍。

    • [函数对象参数],标识一个Lambda的开始,这部分必须存在,不能省略。函数对象参数是传递给编译器自动生成的函数对象类的构造函数的。函数对象参数只能使用那些到定义Lambda为止时Lambda所在作用范围内可见的局部变量(包括Lambda所在类的this)。函数对象参数有以下形式:
      1. 空。没有使用任何函数对象参数。
      2. =。函数体内可以使用Lambda所在作用范围内所有可见的局部变量(包括Lambda所在类的this),并且是值传递方式(相当于编译器自动为我们按值传递了所有局部变量)。
      3. &。函数体内可以使用Lambda所在作用范围内所有可见的局部变量(包括Lambda所在类的this),并且是引用传递方式(相当于编译器自动为我们按引用传递了所有局部变量)。
      4. this。函数体内可以使用Lambda所在类中的成员变量。
      5. a。将a按值进行传递。按值进行传递时,函数体内不能修改传递进来的a的拷贝,因为默认情况下函数是const的。要修改传递进来的a的拷贝,可以添加mutable修饰符。
      6. &a。将a按引用进行传递。
      7. a, &b。将a按值进行传递,b按引用进行传递。
      8. =,&a, &b。除a和b按引用进行传递外,其他参数都按值进行传递。
      9. &, a, b。除a和b按值进行传递外,其他参数都按引用进行传递。
    • (操作符重载函数参数),标识重载的()操作符的参数,没有参数时,这部分可以省略。参数可以通过按值(如:(a,b))和按引用(如:(&a,&b))两种方式进行传递。
    • mutable或exception声明,这部分可以省略。按值传递函数对象参数时,加上mutable修饰符后,可以修改按值传递进来的拷贝(注意是能修改拷贝,而不是值本身)。exception声明用于指定函数抛出的异常,如抛出整数类型的异常,可以使用throw(int)。
    • →返回值类型,标识函数返回值的类型,当返回值为void,或者函数体中只有一处return的地方(此时编译器可以自动推断出返回值类型)时,这部分可以省略。
    • {函数体},标识函数的实现,这部分不能省略,但函数体可以为空。
  • Lambda 函数的声明, 作为变量使用时, 可使用auto或std::function
    auto my_func = [](int x, int y)->int{ return x+y;};
    //或
    std::function<int (int, int)> my_func = [](int x, int y)->int{ return x+y;};
    int result = my_func(1, 2);//result == 3

    而使用变量时, 可使用新增的std::function来声明类型:

    #include <functional>
    int TestFun(int x, int y, std::function<int (int, int)> func) 
    {
        return func(x,y);
    } 
    int result = TestFun(1, 2, my_func);//result == 3
    //std::function 也可以接受函数对象

    或者直接使用模板

    template<Class T>
    int TestFun(int x, int y, T func)
    {
        return T(x, y);
    }
    int result = TestFun(1, 2, my_func);// result == 3
  • 右值引用 (Rvalue Referene) 是C++11引入的新特性 , 它实现了转移语义 (Move Sementics) 和精确传递 (Perfect Forwarding)。主要目的:
    • 消除两个对象交互时不必要的对象拷贝,节省运算存储资源,提高效率。
    • 能够更简洁明确地定义泛型函数。
  • 左值和右值都是针对表达式而言的,左值是指表达式结束后依然存在的持久对象,右值是指表达式结束时就不再存在的临时对象。一个区分左值与右值的便捷方法是:看能不能对表达式取地址,如果能,则为左值,否则为右值。
  • C++ 11中用&表示左值引用,用&&表示右值引用
  • 关于右值引用参数vczh: 参数里面的右值,因为在函数体内部可以被重复引用,所以变成了左值。所以实际上只有调用函数的人认为传进去的参数是右值,而函数体内部还是必须当左值来看待的。这个规则只针对参数成立,因为这是没有办法的。实际上你给一个变量定义为右值类型是说不过去的

  • Singleton,如果需要掌握单例实例的析构时机,使用这个
    static unique_ptr<widget> widget::instance;
    static std::once_flag widget::create;
    widget& widget::get_instance() {
        std::call_once(create, [=]{ instance = make_unique<widget>(); });
        return instance;
    }

    如果不需要,则使用局部静态变量是最完美的方案:

    widget& widget::get_instance() {
        static widget instance;
        return instance;
    }
  • 查看 .so 文件的导出符号: nm -D <name>.soobjdump -tT <name>.so
  • 编译链接时动态库搜索顺序(man ld):The linker uses the following search paths to locate required shared libraries:
    • Any directories specified by -rpath-link options.
    • Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the –with-sysroot option.
    • On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of the environment variable LD_RUN_PATH.
    • On SunOS, if the -rpath option was not used, search any directories specified using -Loptions.
    • For a native linker, the search the contents of the environment variable LD_LIBRARY_PATH.
    • For a native ELF linker, the directories in DT_RUNPATH or DT_RPATH of a shared library are searched for shared libraries needed by it. The DT_RPATH entries are ignored if DT_RUNPATH entries exist.
    • The default directories, normally /lib and /usr/lib.
    • For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file.
  • 由上,如果不想混淆系统lib, 程序可自行用 LD_LIBRARY_PATH 环境变量指定自己的lib目录。
  • 禁用异常的编译选项:-fno-exceptions
  • 有返回值声明的函数末尾如果没有返回, gcc 7.5 版本编译时会默认加个 return 0, 但之后的版本应该是和 clang 一样不加了。c++11 标准,有返回值声明的函数末尾如果没有返回,将出现未定义行为。所以按理这个情况编译器应该报错,但 gcc 编译只报了 warning , 而运行时没有返回值导致的 crash 栈信息都很奇怪不好查。 所以gcc7.5之后的编译参数都得加上 -Werror=return-type ,这样没有返回值时编译期可以报error,好规避问题。
  • gcc/g++ 优化级别详细查看:gcc -Q –help=optimizers -O1
  • CMake - Cross-platform free and open-source software for managing the build process of software using a compiler-independent method. [BSD]
  • Ninja - Ninja is a small build system with a focus on speed.[Apache 2.0], 对标Make, 专注速度
  • autotools
  • meson - Meson is an open source build system meant to be both extremely fast, and, even more importantly, as user friendly as possible. (python)
  • Vcpkg - C++ library manager for Windows, Linux, and MacOS. [MIT]
    • vcpkg 安装的库默认都可以向后兼容,更新方法为直接git pull 最新vcpkg然后 vcpkg update/upgrade, 如果为了协作与稳定,可以指定 git checkout vcpkg 的某个tag, 来保证彼此库版本一致。
    • vcpkg 没有注册表设置或环境变量,卸载直接删除文件夹即可;可在一台计算机上设置任意数量的 vcpkg,它们彼此互不干扰。
  • hunter - CMake driven cross-platform package manager for C/C++
  • :-DConan https://conan.io/ - the C/C++ Package Manager, 结合 JFrog 可构建私有C/C++二进制包管理
  • Folly - Folly is a library of C++14 components designed with practicality and efficiency in mind. Folly contains a variety of core library components used extensively at Facebook. [Apache2]
  • spdlog - Super fast, header only, C++ logging library. [MIT]
  • C++ Actor Framework - An Open Source Implementation of the Actor Model in C++. [BSD-3-Clause]
  • libuv - Cross-platform asynchronous I/O. [BSD]
  • Opus - A totally open, royalty-free, highly versatile audio codec. [BSD]
  • POCO ⚡ - C++ class libraries and frameworks for building network- and internet-based applications that run on desktop, server, mobile and embedded systems. [Boost] website
  • doctest - The lightest feature rich C++ single header testing framework. [MIT]
  • valgrind - A tool for memory debugging, memory leak detection, and profiling.
  • nlohmann-json - JSON for Modern C++. [MIT]
  • Sogou C++ Workflow - This is an enterprise-level programming engine in light and elegant design which can satisfy most C++ back-end development requirements.[Apache-2.0]
  • cpp-httplib - A C++11 single-file header-only cross platform HTTP/HTTPS library.[MIT]
  • public/it/cplusplus.txt
  • 最后更改: 2024/03/15 11:22
  • oakfire