本文介绍了如何限制C ++中与实现相关的语言特性的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是Bjarne Stroustrup的书C ++编程语言的摘录:



第4.6节:

我的问题是:如何限制与实现相关的语言特性的影响?

解决方案

很少有意见:




  • 不幸的是,您必须使用宏来避免某些特定于平台或编译器的问题。你可以看看Boost库的头文件,看看它很容易变得麻烦,例如看下面的文件:








  • 整数类型在不同平台之间往往是混乱的,你必须定义自己的typedef或使用


  • 如果您决定使用任何库,请检查该库是否在给定平台上受支持


  • 具有良好支持和明确记录的平台支持(例如Boost)的库。


  • 您可以通过大量依赖库,其在类型和算法的意义上提供替代。他们还试图使C ++中的编码更便于移植。它工作吗?我不确定。


  • 不是一切都可以用宏来完成。您的构建系统将必须能够检测平台和某些库的存在。许多人会建议 autotools 用于项目配置,我另一方面推荐 CMake (相当不错的语言, c $ c> M4 )


  • endianness和alignment可能是一个问题,如果你做一些低级的介入$ c> reinterpret_cast 和的东西(朋友在C ++上下文中是一个不好的词)。


  • p>为编译器抛出很多警告标志,对于gcc我建议至少 -Wall -Wextra 。但还有更多,请参阅编译器的文档或此问题


  • 您必须注意实现定义和实现依赖的所有内容。



如果你想要真相,只想真相,只要真实,

The following is an excerpt from Bjarne Stroustrup's book, The C++ Programming Language:

Section 4.6:

My question is: How to limit the impact of implementation-dependent language features? Please mention implementation-dependent language features then show how to limit their impact.

解决方案

Few ideas:

  • Unfortunately you will have to use macros to avoid some platform specific or compiler specific issues. You can look at the headers of Boost libraries to see that it can quite easily get cumbersome, for example look at the files:

  • The integer types tend to be messy among different platforms, you will have to define your own typedefs or use something like Boost cstdint.hpp

  • If you decide to use any library, then do a check that the library is supported on the given platform

  • Use the libraries with good support and clearly documented platform support (for example Boost)

  • You can abstract yourself from some C++ implementation specific issues by relying heavily on libraries like Qt, which provide an "alternative" in sense of types and algorithms. They also attempt to make the coding in C++ more portable. Does it work? I'm not sure.

  • Not everything can be done with macros. Your build system will have to be able to detect the platform and the presence of certain libraries. Many would suggest autotools for project configuration, I on the other hand recommend CMake (rather nice language, no more M4)

  • endianness and alignment might be an issue if you do some low level meddling (i.e. reinterpret_cast and things alike (friends was a bad word in C++ context)).

  • throw in a lot of warning flags for the compiler, for gcc I would recommend at least -Wall -Wextra. But there is much more, see the documentation of the compiler or this question.

  • you have to watch out for everything that is implementation-defined and implementation-dependend. If you want the truth, only the truth, nothing but the truth, then go to ISO standard.

这篇关于如何限制C ++中与实现相关的语言特性的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:22