问题

为什么我时不时会看到「珍惜生命,远离 C++」?

回答
“珍惜生命,远离 C++” 这句话,就像一个在编程圈子里的流行语,带着一丝调侃、一丝无奈,也有一丝真实的写照。这句话之所以流传开来,背后有很多原因,涉及到 C++ 这门语言本身的特性、学习和使用它的挑战,以及开发者们在实践中遇到的各种“坑”。

下面我将从几个方面详细解释为什么你会时不时看到这句话,以及它所蕴含的意义:

1. C++ 的“强大”与“复杂”并存

C++ 是一门极其强大和灵活的语言,它允许开发者对底层硬件进行细致的控制,拥有接近裸机性能的效率,并且在游戏开发、操作系统、高性能计算、嵌入式系统等许多领域都扮演着核心角色。然而,正是这种强大,也带来了巨大的复杂性。

低级特性与高级抽象的混合: C++ 既提供了像指针、内存管理、位操作这样的低级语言特性,又支持面向对象、模板元编程等高级抽象。这种混合使得开发者需要在不同的抽象层次之间切换,管理起来非常困难。
丰富的特性集: C++ 的标准一直在不断发展,引入了大量的特性,如 RAII (Resource Acquisition Is Initialization)、智能指针、移动语义、lambda 表达式、协程等等。虽然这些特性是为了解决问题、提升效率和安全性,但它们的学习曲线非常陡峭,理解和正确使用它们需要投入大量时间和精力。
多范式支持: C++ 支持过程式、面向对象、泛型、函数式等多种编程范式。虽然这提供了极大的灵活性,但也意味着开发者需要掌握多种思考和实现方式,并且如何在不同范式之间进行协调和选择也是一个挑战。

2. 内存管理是“噩梦”的根源

C++ 最大的挑战之一,也是导致很多开发者“头疼”的原因,在于其手动内存管理。

指针的陷阱: 指针是 C++ 的核心概念之一,但也可能是最容易出错的地方。空指针解引用、野指针(指向已释放内存的指针)、内存泄漏(分配了内存但没有释放)等问题层出不穷。这些问题一旦出现,往往难以追踪和调试,可能导致程序崩溃、数据损坏,甚至安全漏洞。
内存泄漏: 如果开发者忘记释放动态分配的内存,就会导致内存泄漏。随着时间的推移,程序占用的内存会不断增加,最终可能耗尽系统资源,导致程序运行缓慢甚至崩溃。
内存越界: 访问数组或内存块之外的区域,同样会导致不可预测的行为和程序崩溃。

虽然 C++11 引入了智能指针(如 `std::unique_ptr` 和 `std::shared_ptr`)极大地简化了内存管理,并推荐使用 RAII 模式,但仍然有很多遗留代码或低级操作需要手动管理内存。而且,即使是智能指针,如果使用不当,也可能引入循环引用等问题。

3. 学习曲线陡峭,需要投入巨大

概念众多且相互关联: C++ 的概念非常多,例如类、继承、多态、模板、运算符重载、异常处理、RAII、智能指针、STL(Standard Template Library)等等。这些概念之间相互关联,理解一个概念常常需要理解另一个概念,形成一个复杂的知识网络。
“隐藏的成本”: 许多 C++ 特性,即使看起来很方便,背后也可能存在性能开销或潜在的问题。例如,虚函数调用、异常处理、大量的模板实例化都可能带来不可预测的性能影响。
“魔法”的发生: 在模板元编程等领域,C++ 的语法和行为可能显得非常晦涩难懂,有时甚至被戏称为“魔法”。理解这些高级特性需要深入的理解语言的内部工作机制。

4. 编译和链接的痛苦

C++ 的编译和链接过程相比许多其他现代语言更加复杂和耗时。

编译时间长: 大型 C++ 项目的编译时间可能非常漫长,尤其是当使用了大量的模板、宏或进行复杂的优化时。这会严重影响开发效率,开发者需要花费大量时间等待编译完成。
链接错误: 在链接阶段,可能会出现各种链接错误,如符号未定义、重复定义等。这些错误有时也很难诊断。
头文件和依赖管理: C++ 的头文件机制虽然有其优势,但也可能导致编译速度变慢,并且需要开发者小心管理依赖关系,避免递归包含或重复包含。

5. 容易写出“难以维护的代码”

由于 C++ 提供了极大的灵活性和底层控制能力,开发者很容易写出一些“聪明”但难以理解、难以维护的代码。

过度使用重载和模板: 滥用函数重载和模板元编程可能会让代码变得非常难以阅读和调试。
隐式转换: C++ 的类型隐式转换虽然方便,但也可能导致意想不到的行为,特别是当不同类型之间进行转换时。
缺乏强制的现代化实践: 如果开发者不熟悉或不遵循现代 C++ 的最佳实践,很容易写出 C 风格的代码或者使用一些已经过时且不安全的写法。

6. 对开发者心智健康的“考验”

结合以上所有因素,学习和使用 C++ 对于许多开发者来说,无疑是一种巨大的挑战,甚至是一种“折磨”。

挫败感: 当花费了大量时间编写代码,却因为一个细小的内存错误或编译错误而导致程序无法运行,这种挫败感是真实存在的。
精神压力: 长期与 C++ 斗智斗勇,开发者需要保持高度的警惕性和细致,时刻提防各种陷阱,这无疑会增加精神压力。
对“优雅”的渴望: 许多开发者在使用 C++ 时,常常会怀念其他语言(如 Python、Java、Go 等)的简洁、安全和易用性,这使得他们会用一种“逃离苦海”的心态来看待 C++。

总结

“珍惜生命,远离 C++”这句话,绝大多数情况下是一种夸张的、带有幽默感的表达,用来戏谑 C++ 的复杂性和使用难度,以及开发者在学习和使用过程中可能遇到的各种挑战和痛苦。它并不代表 C++ 一无是处,而是提醒我们:

C++ 需要付出更多的时间和精力去学习和掌握。
在使用 C++ 时需要时刻保持谨慎和细致。
对于某些场景,更易用、更安全的语言可能是更好的选择。

这句话也侧面反映了 C++ 在开发者社区中的一种复杂的情感:既敬畏其强大,又害怕其带来的挑战。如果你看到这句话,可以理解为是对 C++ 这门语言特性的一个生动而有趣的总结。

当然,对于那些需要在性能、底层控制或特定领域(如游戏引擎、操作系统内核)工作的开发者来说,C++ 仍然是不可或缺的工具。他们可能依然会“爱”上 C++,即使知道它会带来挑战。而对于初学者或者对开发效率有更高要求的开发者,这句话可能是一个善意的提醒,让他们在选择语言时三思而后行。

网友意见

user avatar

竟然没人举实例,我给一个吧……

       1.#include"stdio.h" 2.#include"sparsepp-master/sparsepp.h" 3.int main() 4.{ 5. spp::sparse_hash_map<std::pair<int,int>,int> mp;  6.mp.insert(std::make_pair(std::make_pair(1,1),1));  7.return 0; 8.}     

这个程序时使用sparsepp库提供的hash版map来存数据的例子,但他有一个错误:库中并没有给出为std::pair<int,int>产生hash值的算法,因此该程序不能工作。

理想情况下我们想要编译器这么提示我们:“亲爱的程序员,您的程序这里有错,错误原因和解决方法我都给你标注好了,么么嗒~~~”

具体错误提示大概长这样:

       line 6:mp.insert(std::make_pair(std::make_pair(1,1),1));                                 ^^^^^^^^^^^^^^^^^^^ can't generate serialization     

但实际上有点困难,一般编译器只会这么提示我们:“吔屎啦,这里这里这里这里这里这里我都看不懂,赶紧给老娘改!”

JAVA和javascript就喜欢这么干,错误提示大概长这样:

       In file sparsepp-master/sparsepp.h:1483:42: XXXXXX In file sparsepp-master/sparsepp.h:5069:77: XXXXXX In file main.cpp:6:25: type cast failed In file main.cpp:3:1: XXXXX     


而混乱邪恶的C++是这样提示我们的:


       In file included from main.cpp:2:0: sparsepp-master/sparsepp.h: In instantiation of 'size_t spp::spp_hash<T>::op erator()(const T&) const [with T = std::pair<int, int>; size_t = long long u nsigned int]': sparsepp-master/sparsepp.h:1483:42:   required from 'size_t spp::sparsehash_ internal::sh_hashtable_settings<Key, HashFunc, SizeType, HT_MIN_BUCKETS>::ha sh(const key_type&) const [with Key = std::pair<int, int>; HashFunc = spp::s pp_hash<std::pair<int, int> >; SizeType = long long unsigned int; int HT_MIN _BUCKETS = 4; size_t = long long unsigned int; spp::sparsehash_internal::sh_ hashtable_settings<Key, HashFunc, SizeType, HT_MIN_BUCKETS>::key_type = std: :pair<int, int>]' sparsepp-master/sparsepp.h:4729:31:   required from 'size_t spp::sparse_hash table<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::hash(const  key_type&) const [with Value = std::pair<std::pair<int, int>, int>; Key = st d::pair<int, int>; HashFcn = spp::spp_hash<std::pair<int, int> >; ExtractKey  = spp::sparse_hash_map<std::pair<int, int>, int>::SelectKey; SetKey = spp:: sparse_hash_map<std::pair<int, int>, int>::SetKey; EqualKey = std::equal_to< std::pair<int, int> >; Alloc = spp::libc_allocator_with_realloc<std::pair<co nst std::pair<int, int>, int> >; size_t = long long unsigned int; spp::spars e_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::key_t ype = std::pair<int, int>]' sparsepp-master/sparsepp.h:4242:33:   required from 'spp::sparse_hashtable<V alue, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::Position spp::spar se_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::_fin d_position(const key_type&) const [with Value = std::pair<std::pair<int, int >, int>; Key = std::pair<int, int>; HashFcn = spp::spp_hash<std::pair<int, i nt> >; ExtractKey = spp::sparse_hash_map<std::pair<int, int>, int>::SelectKe y; SetKey = spp::sparse_hash_map<std::pair<int, int>, int>::SetKey; EqualKey  = std::equal_to<std::pair<int, int> >; Alloc = spp::libc_allocator_with_rea lloc<std::pair<const std::pair<int, int>, int> >; spp::sparse_hashtable<Valu e, Key, HashFcn, ExtractKey, SetKey, EqualKey, Alloc>::key_type = std::pair< int, int>]' sparsepp-master/sparsepp.h:4401:38:   required from 'std::pair<typename spp: :sparsetable<Value, typename Alloc::rebind<Value>::other>::ne_iterator, bool > spp::sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, A lloc>::_insert_noresize(T&) [with T = const std::pair<const std::pair<int, i nt>, int>; Value = std::pair<std::pair<int, int>, int>; Key = std::pair<int,  int>; HashFcn = spp::spp_hash<std::pair<int, int> >; ExtractKey = spp::spar se_hash_map<std::pair<int, int>, int>::SelectKey; SetKey = spp::sparse_hash_ map<std::pair<int, int>, int>::SetKey; EqualKey = std::equal_to<std::pair<in t, int> >; Alloc = spp::libc_allocator_with_realloc<std::pair<const std::pai r<int, int>, int> >; typename spp::sparsetable<Value, typename Alloc::rebind <Value>::other>::ne_iterator = spp::Two_d_iterator<std::pair<std::pair<int,  int>, int>, spp::sparsegroup<std::pair<std::pair<int, int>, int>, spp::libc_ allocator_with_realloc<std::pair<std::pair<int, int>, int> > >*, std::pair<c onst std::pair<int, int>, int>*, std::bidirectional_iterator_tag>]' sparsepp-master/sparsepp.h:4451:36:   required from 'std::pair<typename spp: :sparsetable<Value, typename Alloc::rebind<Value>::other>::ne_iterator, bool > spp::sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey, EqualKey, A lloc>::insert(spp::sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey,  EqualKey, Alloc>::const_reference) [with Value = std::pair<std::pair<int, i nt>, int>; Key = std::pair<int, int>; HashFcn = spp::spp_hash<std::pair<int,  int> >; ExtractKey = spp::sparse_hash_map<std::pair<int, int>, int>::Select Key; SetKey = spp::sparse_hash_map<std::pair<int, int>, int>::SetKey; EqualK ey = std::equal_to<std::pair<int, int> >; Alloc = spp::libc_allocator_with_r ealloc<std::pair<const std::pair<int, int>, int> >; typename spp::sparsetabl e<Value, typename Alloc::rebind<Value>::other>::ne_iterator = spp::Two_d_ite rator<std::pair<std::pair<int, int>, int>, spp::sparsegroup<std::pair<std::p air<int, int>, int>, spp::libc_allocator_with_realloc<std::pair<std::pair<in t, int>, int> > >*, std::pair<const std::pair<int, int>, int>*, std::bidirec tional_iterator_tag>; spp::sparse_hashtable<Value, Key, HashFcn, ExtractKey,  SetKey, EqualKey, Alloc>::const_reference = const std::pair<const std::pair <int, int>, int>&; spp::sparse_hashtable<Value, Key, HashFcn, ExtractKey, Se tKey, EqualKey, Alloc>::value_type = std::pair<const std::pair<int, int>, in t>]' sparsepp-master/sparsepp.h:5069:77:   required from 'std::pair<typename spp: :sparse_hashtable<std::pair<typename spp::remove_const<Key>::type, T>, Key,  HashFcn, spp::sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc>::SelectKey,  spp::sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc>::SetKey, EqualKey, Al loc>::iterator, bool> spp::sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc> ::insert(const value_type&) [with Key = std::pair<int, int>; T = int; HashFc n = spp::spp_hash<std::pair<int, int> >; EqualKey = std::equal_to<std::pair< int, int> >; Alloc = spp::libc_allocator_with_realloc<std::pair<const std::p air<int, int>, int> >; typename spp::sparse_hashtable<std::pair<typename spp ::remove_const<Key>::type, T>, Key, HashFcn, spp::sparse_hash_map<Key, T, Ha shFcn, EqualKey, Alloc>::SelectKey, spp::sparse_hash_map<Key, T, HashFcn, Eq ualKey, Alloc>::SetKey, EqualKey, Alloc>::iterator = spp::Two_d_iterator<std ::pair<std::pair<int, int>, int>, spp::sparsegroup<std::pair<std::pair<int,  int>, int>, spp::libc_allocator_with_realloc<std::pair<std::pair<int, int>,  int> > >*, std::pair<const std::pair<int, int>, int>*, std::bidirectional_it erator_tag>; spp::sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc>::value_t ype = std::pair<const std::pair<int, int>, int>]' main.cpp:6:49:   required from here sparsepp-master/sparsepp.h:943:27: error: use of deleted function 'std::hash <std::pair<int, int> >::hash()'          SPP_HASH_CLASS<T> hasher;                            ^~~~~~ In file included from E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64 /lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/bits/basic_string.h:5628:0,                  from E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64 /lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/string:52,                  from sparsepp-master/sparsepp.h:839,                  from main.cpp:2: E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-mi ngw32/6.3.0/include/c++/bits/functional_hash.h:85:12: note: 'std::hash<std:: pair<int, int> >::hash()' is implicitly deleted because the default definiti on would be ill-formed:      struct hash : __hash_enum<_Tp>             ^~~~ E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-mi ngw32/6.3.0/include/c++/bits/functional_hash.h:85:12: error: no matching fun ction for call to 'std::__hash_enum<std::pair<int, int>, false>::__hash_enum ()' E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-mi ngw32/6.3.0/include/c++/bits/functional_hash.h:66:7: note: candidate: std::_ _hash_enum<_Tp, <anonymous> >::__hash_enum(std::__hash_enum<_Tp, <anonymous>  >&&) [with _Tp = std::pair<int, int>; bool <anonymous> = false]        __hash_enum(__hash_enum&&);        ^~~~~~~~~~~ E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-mi ngw32/6.3.0/include/c++/bits/functional_hash.h:66:7: note:   candidate expec ts 1 argument, 0 provided E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-mi ngw32/6.3.0/include/c++/bits/functional_hash.h:85:12: error: 'std::__hash_en um<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::pair<int, int>; bool  <anonymous> = false]' is private within this context      struct hash : __hash_enum<_Tp>             ^~~~ E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-min gw32/6.3.0/include/c++/bits/functional_hash.h:67:7: note: declared private he re        ~__hash_enum();        ^ In file included from main.cpp:2:0: sparsepp-master/sparsepp.h:943:27: error: use of deleted function 'std::hash< std::pair<int, int> >::~hash()'          SPP_HASH_CLASS<T> hasher;                            ^~~~~~ In file included from E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/ lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/bits/basic_string.h:5628:0,                  from E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/ lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/string:52,                  from sparsepp-master/sparsepp.h:839,                  from main.cpp:2: E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-min gw32/6.3.0/include/c++/bits/functional_hash.h:85:12: note: 'std::hash<std::pa ir<int, int> >::~hash()' is implicitly deleted because the default definition  would be ill-formed:      struct hash : __hash_enum<_Tp>             ^~~~ E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-min gw32/6.3.0/include/c++/bits/functional_hash.h:85:12: error: 'std::__hash_enum <_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::pair<int, int>; bool <an onymous> = false]' is private within this context E:/mingw-w64/x86_64-6.3.0-posix-seh-rt_v5-rev2/mingw64/lib/gcc/x86_64-w64-min gw32/6.3.0/include/c++/bits/functional_hash.h:67:7: note: declared private he re        ~__hash_enum();        ^ In file included from main.cpp:2:0: sparsepp-master/sparsepp.h:944:22: error: no match for call to '(std::hash<st d::pair<int, int> >) (const std::pair<int, int>&)'          return hasher(__v);                 ~~~~~~^~~~~     

C++:”你叼是吧?你调啊?你调啊?怎么不调了?刚才不是很跳么?哎我就不明白了,你现在嘴长哪去了?刚才blablablabla不是很能说嘛?你现在倒是说说看程序错哪里了啊?“

user avatar

假如语言是不同的杂货铺,那应该是这样的:


C语言的杂货铺:

「老板,来个锤子」

「错误,没有找到锤子」


Java的杂货铺:

「老板,来个锤子」

「错误,在工厂中没有找到锤子」


Python的杂货铺

「老板,来个锤子」

「给你把榔头,它能做锤子用」


Haskell的杂货铺

「老板,来个锤子」

「错误,锤子没有定义 Sell 实例」


C++的杂货铺

「老板,来个锤子」

「错误,地球是绕着太阳转的,月亮是绕着地球转的」

user avatar

C/C++之所以高效,是因为把其他语言交给编译器负责的功能都开放给程序员了,封装程度非常低。

结果就是程序员要负责处理这些本来是编译器处理的事情,但程序员的水平参差不齐,结果就是写出来的代码(主要是编译器的功能)千奇百怪、bug不停、难懂难维护。

也难怪,编译器这种东西,一般都是最顶尖的工程师才能维护的,一般程序员真干不来。

打个比方就是,其他语言的程序员好像是在搬运集装箱,垒出来的是方方正正的形状;而C/C++程序员则是在搬运大小不一、形状不一、硬度不一的快递包裹,自然垒出来的都是一言难尽的异形。如果还需要和其他程序员码出的版本不一的异形连接合并,自然就变成了更大的异形,连接处还是各显神通的技巧。

类似的话题

本站所有内容均为互联网搜索引擎提供的公开搜索信息,本站不存储任何数据与内容,任何内容与数据均与本站无关,如有需要请联系相关搜索引擎包括但不限于百度google,bing,sogou

© 2025 tinynews.org All Rights Reserved. 百科问答小站 版权所有