简体   繁体   English

C ++标准库可移植性

[英]C++ Standard Library Portability

I work on large scale, multi platform, real time networked applications. 我从事大规模,多平台,实时网络应用。 The projects I work on lack any real use of containers or the Standard Library in general, no smart pointers or really any "modern" C++ language features. 我工作的项目一般没有真正使用容器或标准库,没有智能指针或任何“现代”C ++语言功能。 Lots of raw dynamically allocated arrays are common place. 大量原始动态分配的阵列很常见。

I would very much like to start using the Standard Library and some of the C++11 spec, however, there are many people also working on my projects that are against because "STL / C++11 isn't as portable, we take a risk using it". 我非常想开始使用标准库和一些C ++ 11规范,然而,有很多人也在研究我的项目,因为“STL / C ++ 11不那么便携,我们冒风险使用它“。 We do run software on a wide variety of embedded systems as well as fully fledged Ubuntu/Windows/Mac OS systems. 我们在各种嵌入式系统以及完全成熟的Ubuntu / Windows / Mac OS系统上运行软件。

So, to my question; 那么,对我的问题; what are the actual issues of portability with concern to the Standard Library and C++11? 标准库和C ++ 11关注的可移植性的实际问题是什么? Is it just a case of having g++ past a certain version? 这只是让g ++超过某个版本的情况吗? Are there some platforms that have no support? 有些平台没有支持吗? Are compiled libraries required and if so, are they difficult to obtain/compile? 是否需要编译库?如果需要,是否难以获取/编译? Has anyone had serious issues being burnt by non-portable pure C++? 有没有人有严重的问题被非便携式纯C ++烧毁?

Library support for the new C++11 Standard is pretty complete for either Visual C++ 2012, gcc >= 4.7 and Clang >= 3.1, apart from some concurrency stuff. 对于Visual C ++ 2012,gcc> = 4.7和Clang> = 3.1,新的C ++ 11标准的库支持非常完整,除了一些并发的东西。 Compiler support for all the individual language features is another matter. 编译器支持所有单个语言功能是另一回事。 See this link for an up to date overview of supported C++11 features. 有关受支持的C ++ 11功能的最新概述,请参阅此链接

For an in-depth analysis of C++ in an embedded/real-time environment, Scott Meyers's presentation materials are really great. 为了在嵌入式/实时环境中深入分析C ++,Scott Meyers的演示材料非常棒。 It discusses costs of virtual functions, exception handling and templates, and much more. 它讨论了虚拟功能,异常处理和模板的成本等等。 In particular, you might want to look at his analysis of C++ features such as heap allocations, runtime type information and exceptions, which have indeterminate worst-case timing guarantees , which matter for real-time systems. 特别是,您可能希望查看他对C ++特性的分析,例如堆分配,运行时类型信息和异常,它们具有不确定的最坏情况时序保证 ,这对于实时系统很重要。

It's those kind of issues and not portability that should be your major concern (if you care about your granny's pacemaker...) 这是一个问题,而不是可移植性应该是你的主要关注点(如果你关心你的奶奶的心脏起搏器......)

Any compiler for C++ should support some version of the standard library. 任何C ++编译器都应该支持某些版本的标准库。 The standard library is part of C++. 标准库是C ++的一部分。 Not supporting it means the compiler is not a C++ compiler. 不支持它意味着编译器不是C ++编译器。 I would be very surprised if any of the compilers you're using at the moment don't portably support the C++03 standard library, so there's no excuse there. 如果您目前使用的任何编译器都不支持C ++ 03标准库,我会非常惊讶,因此没有任何借口。 Of course, the compiler will have to be have been updated since 2003, but unless you're compiling for some archaic system that is only supported by an archaic compiler, you'll have no problems. 当然,编译器必须自2003年以来一直在更新,但除非你正在编译一些仅由古老的编译器支持的古老系统,否则你将没有问题。

As for C++11, support is pretty good at the moment. 至于C ++ 11,目前支持非常好。 Both GCC and MSVC have a large portion of the C++11 standard library supported already. GCC和MSVC都已经支持了很大一部分C ++ 11标准库。 Again, if you're using the latest versions of these compilers and they support the systems you want to compile for, then there's no reason you can't use the subset of the C++11 standard library that they support - which is almost all of it. 同样,如果您正在使用这些编译器的最新版本并且它们支持您要编译的系统,那么您没有理由不能使用它们支持的C ++ 11标准库的子集 - 这几乎是所有的。

C++ without the standard library just isn't C++. 没有标准库的C ++就不是C ++。 The language and library features go hand in hand. 语言和库功能齐头并进。

There are lists of supported C++11 library features for GCC's libstdc++ and MSVC 2012 . GCC的libstdc ++MSVC 2012支持的C ++ 11库特性列表。 I can't find anything similar for LLVM's libc++, but they do have a clang c++11 support page . 我找不到类似LLVM的libc ++,但他们确实有一个clang c ++ 11支持页面

The people you are talking to are confusing several different issues. 你正在谈论的人混淆了几个不同的问题。 C++11 isn't really portable today. C ++ 11今天真的不便携。 I don't think any compiler supports it 100% (although I could be wrong); 我不认为任何编译器100%支持它(虽然我可能是错的); you can get away with using large parts of it if (and only if) you limit yourself to the most recent compilers on two or three platforms (Windows and Linux, and probably Apple). 如果(并且仅当)你将自己限制在两个或三个平台(Windows和Linux,可能还有Apple)上的最新编译器,你就可以使用它的大部分内容。 While these are the most visible platforms, they represent but a small part of all machines. 虽然这些是最明显的平台,但它们只占所有机器的一小部分。 (If you're working on large scale networked applications, Solaris will probably be important, and Sun CC. Unless Sun have greatly changed since I last worked on it, that means that there are even parts of C++03 that you can't count on.) (如果你正在研究大规模的网络应用程序,那么Solaris可能很重要,而且Sun CC。除非Sun自上次工作以来已经发生了很大变化,否则这意味着甚至可以使用C ++ 03的部分内容。 t指望。)

The STL is a completely different issue. STL是一个完全不同的问题。 It depends partially on what you mean by the STL, but there is certainly no portability problem today in using std::vector . 它部分取决于你对STL的意思,但是今天使用std::vector肯定没有可移植性问题。 locale might be problematic on a very few compilers (it was with Sun CC—with both the Rogue Wave and the Stlport libraries), and some of the algorithms, but for the most part, you can pretty much count on all of C++03. locale可能在很少的编译器上出现问题(它与Sun CC一起使用Rogue Wave和Stlport库)和一些算法,但在大多数情况下,你几乎可以依赖所有的C ++ 03。

And in the end, what are the alternatives? 最后,有哪些替代方案? If you don't have std::vector , you end up implementing something pretty much like it. 如果你没有std::vector ,你最终会实现与它非常相似的东西。 If you're really worried about the presence of std::vector , wrap it in your own class—if ever it's not available (highly unlikely, unless you go back with a time machine), just reimplement it, exactly like we did in the pre-standard days. 如果你真的担心std::vector的存在,把它包装在你自己的类中 - 如果它不可用(极不可能,除非你带回时间机器),只需重新实现它,就像我们在标准前的日子。

Use STLPort with your existing compiler, if it supports it . 如果支持 ,请将STLPort与现有编译器一起使用。 This is no more than a library of code, and you use other libraries without problem, right? 这只不过是一个代码库,你可以毫无问题地使用其他库,对吧?

Every permitted implementation-defined behaviour is listed in publicly available standard draft . 每个允许的实现定义的行为都列在公开的标准草案中 There is next to nothing less portable in C+11 than in C++98. C + 11中的可移植性几乎与C ++ 98中的可移植性相差无几。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM