public:lang:cplusplus

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
public:lang:cplusplus [2026/05/07 15:06] – 移除 - 外部编辑 (未知日期) 127.0.0.1public:lang:cplusplus [2026/05/07 15:47] (当前版本) – ↷ 链接因页面移动而自动修正 51.75.119.168
行 1: 行 1:
 +====== C++ ======
 +<WRAP center tip 60%>
 +**Useful C/C++ related websites.**
 +  * [[http://www.open-std.org/JTC1/SC22/WG21/|The C++ Standards Committee - ISOCPP]]
 +  * [[http://isocpp.org/|Standard C++]] 8-) - News, Status & Discussion about Standard C++.
 +  * [[http://cppcon.org/|CppCon]] - The C++ Conference.
 +  * [[http://cppreference.com/|C++ reference]] - C++98, C++03, C++11, C++14 reference.
 +  * [[http://www.cplusplus.com|CPlusPlus.com]] - The C++ Resources Network.
 +  * [[http://meetingcpp.com/|Meeting C++]]
 +  * [[https://en.cppreference.com/w/cpp/compiler_support|编译器支持情况]]
 +
 +**Useful C/C++ related weblogs.**
 +
 +  * [[http://codingforspeed.com/|Coding For Speed]] - Coding For Speed DOT COM, Less Execution Time.
 +  * [[http://ericniebler.com/|Eric Niebler]]
 +  * [[http://blog.feabhas.com/|Sticky Bits]]
 +  * [[http://pfultz2.com/blog/|Paul Fultz II's Blog]]
 +  * [[http://www.cnblogs.com/miloyip/archive/2010/09/17/behind_cplusplus.html|C++强大背后--Milo]]
 +  * [[https://herbsutter.com/|Herb Sutter's Blog]]
 +
 +</WRAP>
 +
 +----
 +===== Articles =====
 +  * <del>[[http://codemacro.com/2014/09/02/stack-frame/|手动调用堆栈]]</del>
 +  * [[public:lang:cplusplus:effective_modern_cplusplus]]
 +  * [[http://mindhacks.cn/2012/08/27/modern-cpp-practices/|modern cpp practices]]
 +  * [[https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms|c++ 惯用法]]
 +  * [[http://blog.guorongfei.com/2018/11/24/static-analizer/|c++ 代码检查工具]]. [[https://github.com/cpplint/cpplint|cpplint]], cppcheck, [[http://clang.llvm.org/extra/clang-tidy/|clang-dity]]
 +  * [[https://github.com/changkun/modern-cpp-tutorial/blob/master/README-zh-cn.md|Modern cpp tutorial]]
 +  * [[https://google.github.io/styleguide/cppguide.html|Google C++ Style Guide]]
 +  * [[https://github.com/google/sanitizers/wiki/AddressSanitizer|AddressSanitizer]] 内存错误检测:-o
 +    * Meson 构建系统打开 ASAN 的方式:[[https://mesonbuild.com/Builtin-options.html#base-options|设置 b_sanitize]]
 +  * [[https://agraphicsguynotes.com/posts/fiber_in_cpp_understanding_the_basics/|Fiber in C++: 纤程的原理与实现]]
 +  * [[https://learnmoderncpp.com/|Learn Modern C++]]
 +  * [[https://blogs.gentoo.org/mgorny/2024/09/23/overview-of-cross-architecture-portability-problems/|C的移植性问题,参考]]
 +
 +===== Syntax =====
 +  * [[http://www.cnblogs.com/Solstice/archive/2011/07/17/2108715.html|iostream]]
 +  * 凡是表达“只读”语义的场景都使用 ''const'',表达“常量”语义的场景都使用 ''constexpr'';
 +  * 成员函数名后的修饰词,除了''const'',''volitile'', c++ 11之后新增了 ''&'', ''&&'', ''override'', ''final'', ''noexcept'',其中 ''&''与''&&''称为引用限定符,限制调用该成员函数的this是左值还是右值,详见 c++ primer 5
 +==== › STL ====
 +  * 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 的线程安全:[[https://www.boost.org/doc/libs/1_73_0/libs/smart_ptr/doc/html/smart_ptr.html#shared_ptr_thread_safety|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, 基于[[https://docs.python.org/3/library/string.html#formatspec|python格式化规范]]
 +    * 可中断线程  std::jthread, std::stop_token
 +    * 并发控制 std::barrier, std::latch
 +    * 协程关键字 co_await, co_yield, co_return
 +    * 操作 std::ranges, std::views
 +    * 原子引用 std::atomic_ref
 +    * 数学常数 std::numbers
 +    * 代码位置 std::source_location
 +=== › › condition_variable ===
 +  * [[https://en.cppreference.com/w/cpp/thread/condition_variable|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 ===
 +  * ''vector<bool>''既不是 vector, 也不存 bool, 更不是容器。C++98 起就对它进行按位存储优化空间,导致行为差异。
 +  * ''std::ios::binary'' [[https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary|可能达不到你想要的效果]]
 +  * ''std::copy'' 性能可能比 ''memcpy'' 更好: https://stackoverflow.com/questions/4707012/is-it-better-to-use-stdmemcpy-or-stdcopy-in-terms-to-performance
 +==== › Meta Pragram ====
 +  * {{:public:it:ccc.ppt| 模​板​元​编​程​技​术​与​应​用​}}
 +
 +==== › Lambda ====
 +  * [[http://en.cppreference.com/w/cpp/language/lambda| cppreference.com的lambda词条]]
 +  * [[http://hczhcz.github.io/2014/11/04/type-and-storage-of-lambda.html | 关于Lambda函数的存储]] <wrap em>lambda相当于保存在堆上的函数对象</wrap>
 +  * 以下拷贝于[[http://www.cnblogs.com/hujian/archive/2012/02/14/2350306.html | C++ 11 Lambda表达式]]
 +  * C++ 11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作。Lambda的语法形式如下:<code>[函数对象参数] (操作符重载函数参数) mutable或exception声明 ->返回值类型 {函数体}</code>可以看到,Lambda主要分为五个部分,下面分别进行介绍。
 +      * ''[函数对象参数]'',标识一个Lambda的开始,这部分必须存在,不能省略。函数对象参数是传递给编译器自动生成的函数对象类的构造函数的。函数对象参数只能使用那些到定义Lambda为止时Lambda所在作用范围内可见的局部变量(包括Lambda所在类的this)。函数对象参数有以下形式:
 +           - 空。没有使用任何函数对象参数。
 +           - =。函数体内可以使用Lambda所在作用范围内所有可见的局部变量(包括Lambda所在类的this),并且是值传递方式(相当于编译器自动为我们按值传递了所有局部变量)。
 +           - &。函数体内可以使用Lambda所在作用范围内所有可见的局部变量(包括Lambda所在类的this),并且是引用传递方式(相当于编译器自动为我们按引用传递了所有局部变量)。
 +           - this。函数体内可以使用Lambda所在类中的成员变量。
 +           - a。将a按值进行传递。按值进行传递时,函数体内不能修改传递进来的a的拷贝,因为默认情况下函数是const的。要修改传递进来的a的拷贝,可以添加mutable修饰符。
 +           - &a。将a按引用进行传递。
 +           - a, &b。将a按值进行传递,b按引用进行传递。
 +           - =,&a, &b。除a和b按引用进行传递外,其他参数都按值进行传递。
 +           - &, a, b。除a和b按值进行传递外,其他参数都按引用进行传递。
 +      * ''(操作符重载函数参数)'',标识重载的()操作符的参数,没有参数时,这部分可以省略。参数可以通过按值(如:(a,b))和按引用(如:(&a,&b))两种方式进行传递。
 +      * ''mutable或exception声明'',这部分可以省略。按值传递函数对象参数时,加上mutable修饰符后,可以修改按值传递进来的拷贝(注意是能修改拷贝,而不是值本身)。exception声明用于指定函数抛出的异常,如抛出整数类型的异常,可以使用throw(int)。
 +      * ''->返回值类型'',标识函数返回值的类型,当返回值为void,或者函数体中只有一处return的地方(此时编译器可以自动推断出返回值类型)时,这部分可以省略。
 +      * ''{函数体}'',标识函数的实现,这部分不能省略,但函数体可以为空。
 +  * Lambda 函数的声明, 作为变量使用时, 可使用auto或std::function<code c++>
 +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
 +</code>  而使用变量时, 可使用新增的std::function来声明类型:<code c++>
 +#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 也可以接受函数对象
 +</code>或者直接使用模板<code c++>
 +template<Class T>
 +int TestFun(int x, int y, T func)
 +{
 +    return T(x, y);
 +}
 +int result = TestFun(1, 2, my_func);// result == 3
 +</code>
 +
 +==== › std::function ====
 +  * [[http://www.cnblogs.com/hujian/archive/2012/12/07/2807605.html | c++ 11 function]]
 +  * [[http://en.cppreference.com/w/cpp/utility/functional/function | cppreference ]]
 +==== › 右值引用 (Rvalue Referene) ====
 +  * <del>[[http://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/index.html 右值引用与转移语义]]</del>
 +  * 右值引用 (Rvalue Referene) 是C++11引入的新特性 , 它实现了转移语义 (Move Sementics) 和精确传递 (Perfect Forwarding)。主要目的:
 +     * 消除两个对象交互时不必要的对象拷贝,节省运算存储资源,提高效率。
 +     * 能够更简洁明确地定义泛型函数。
 +  * 左值和右值都是针对表达式而言的,左值是指表达式结束后依然存在的持久对象,右值是指表达式结束时就不再存在的临时对象。一个区分左值与右值的便捷方法是:看能不能对表达式取地址,如果能,则为左值,否则为右值。
 +  * C++ 11中用''&''表示左值引用,用''&&''表示右值引用
 +  * [[http://www.zhihu.com/question/27021041#answer-9358391| 关于右值引用参数]]vczh: 参数里面的右值,因为在函数体内部可以被重复引用,所以变成了左值。所以实际上只有调用函数的人认为传进去的参数是右值,而函数体内部还是必须当左值来看待的。这个规则只针对参数成立,因为这是没有办法的。实际上你给一个变量定义为右值类型是说不过去的
 +
 +----
 +===== Pattern =====
 +  * [[https://www.cnblogs.com/zxh1210603696/p/4157294.html|Singleton]],如果需要掌握单例实例的析构时机,使用这个<code c++>
 +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;
 +}</code>如果不需要,则使用局部静态变量是最完美的方案:<code c++>
 +widget& widget::get_instance() {
 +    static widget instance;
 +    return instance;
 +}
 +</code>
 +===== Linux 下编译 =====
 +  * 查看 ''.so'' 文件的导出符号: ''nm -D <name>.so'' 或 ''objdump -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目录。
 +==== › Tips ====
 +  * 禁用异常的编译选项:''-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''
 +
 +
 +===== C++资源与工具 =====
 +  * [[https://github.com/fffaraz/awesome-cpp|awesome-cpp]]
 +  * [[https://code.woboq.org/|C++代码在线查看器]],功能很不错,和在IDE上看差别不大
 +  * [[..:it:gdb]]
 +==== › Build System ====
 +  * [[..:it:cmake]]  - Cross-platform free and open-source software for managing the build process of software using a compiler-independent method. [BSD]
 +  * [[https://ninja-build.org/|Ninja]] - Ninja is a small build system with a focus on speed.[Apache 2.0], 对标Make, 专注速度
 +  * autotools
 +  * [[..:it:gyp]]
 +  * [[https://mesonbuild.com/|meson]] - Meson is an open source build system meant to be both extremely fast, and, even more importantly, as user friendly as possible. (python)
 +==== › 包管理 ====
 +  * [[https://github.com/microsoft/vcpkg|Vcpkg]] - C++ library manager for Windows, Linux, and MacOS. [MIT]
 +    * vcpkg 安装的库默认都可以向后兼容,更新方法为直接git pull 最新vcpkg然后 vcpkg update/upgrade, 如果为了协作与稳定,可以指定 git checkout vcpkg 的某个tag, 来保证彼此库版本一致。
 +    * vcpkg 没有注册表设置或环境变量,卸载直接删除文件夹即可;可在一台计算机上设置任意数量的 vcpkg,它们彼此互不干扰。
 +  * [[https://github.com/cpp-pm/hunter|hunter]] - CMake driven cross-platform package manager for C/C++
 +  * :-D[[.cplusplus:conan]] [[https://conan.io/]] - the C/C++ Package Manager, 结合 [[https://jfrog.com/|JFrog]] 可构建私有C/C++二进制包管理
 +
 +==== › 用到的 ====
 +  * [[https://github.com/facebook/folly|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]
 +  * [[https://github.com/gabime/spdlog|spdlog]] - Super fast, header only, C++ logging library. [MIT]
 +  * [[https://github.com/actor-framework/actor-framework|C++ Actor Framework]] - An Open Source Implementation of the Actor Model in C++. [BSD-3-Clause]
 +  * [[https://github.com/libuv/libuv|libuv]] - Cross-platform asynchronous I/O. [BSD]
 +  * [[http://opus-codec.org/|Opus]] - A totally open, royalty-free, highly versatile audio codec. [BSD]
 +  * [[https://github.com/pocoproject|POCO]] ⚡ - C++ class libraries and frameworks for building network- and internet-based applications that run on desktop, server, mobile and embedded systems. [Boost] [[http://pocoproject.org/|website]]
 +  * [[..:it:protobuff]]
 +  * [[https://github.com/onqtam/doctest|doctest]] - The lightest feature rich C++ single header testing framework. [MIT]
 +  * [[http://valgrind.org/|valgrind]] - A tool for memory debugging, memory leak detection, and profiling.
 +  * [[https://github.com/nlohmann/json|nlohmann-json]] -  JSON for Modern C++. [MIT]
 +  * [[https://github.com/sogou/workflow|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]
 +  * [[https://github.com/yhirose/cpp-httplib|cpp-httplib]] - A C++11 single-file header-only cross platform HTTP/HTTPS library.[MIT]
 +