使用较新的C ++标准的反向移植类型和模板扩展名称空间std [英] Extending namespace std with backported types and templates from newer C++ standards

查看:76
本文介绍了使用较新的C ++标准的反向移植类型和模板扩展名称空间std的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们正在开发一个允许在可能的情况下使用C ++ 11/14功能和类型的库,但是必须回退到C ++ 03实现。鼓励该库的用户和开发人员使用命名空间 std 中的类型和模板。

Currently we work on a library that is allowed to use C++11/14 features and types when possible but must fallback to a C++03 implementation otherwise. Users and developers of this library are encouraged to use types and templates from namespace std.

使用类型时会出现问题来自由较新版本的C ++标准引入的名称空间 std ,该版本未随所使用的编译器一起提供。建议的一种方法是为某些不依赖新语言功能的类型提供 backports作为命名空间 std 作为我们库代码的一部分,例如固定宽度整数类型

Problems arise when using types from namespace std introduced by newer versions of the C++ Standard which are not shipped with the compiler used. One suggested option is to provide 'backports' for certain types that do not rely on new language features into namespace std as part of our library code, e.g. Fixed width integer types:

//file: std_backport.h

namespace std_backport {
    typedef char  int8_t;  //for platforms where 'char' is 8 bit
    typedef short int16_t; //for platforms where 'short' is 16 bit
    //[and so on]
}

//enter 'sacred' territory and introduce the types to namespace std
namespace std {
    using namespace ::std_backport;
}

另一个例子是 tuple 类型:

//file std_tuple_backport.h
#include <tr1/tuple>                //std::tr1::tuple

namespace std {
    using ::std::tr1::tuple;
}

当然,我们必须提供检查以启用/禁用这些文件的包含根据使用的编译器。

Of course we have to provide checks to enable/disable the inclusion of those files according to the compilers used.

我对此事进行了一些研究,并意识到了扩展名称空间std 就像是未定义的行为,我想放弃这个想法。但是比我遇到的这个答案(在#4中指出)

I did some research on this matter and aware of the fact, that extending the namespace std like this is undefined behavior I wanted to drop this idea. But than I came across this answer which states in #4:

在std名称空间中放置任何内容都是未定义的行为。这意味着规范没有说明将会发生什么。但是,如果您知道在特定平台上标准库未定义某些内容,则继续进行定义。 [...]

Putting anything in std namespace is an "undefined behaviour". That means the specification does not say what will happen. But if you know that on particular platform the standard library does not define something, just go ahead and define it. [...]

所以我的问题是:


  • 在这种情况下,能否请人向我澄清未定义行为的确切含义?

  • 是否正在提供C ++ 11/14兼容性,就像这样的不良做法 '即使我们只是从更新的标准中提供类型?

  • 在无需定义名称空间<$ c $的所有内容的情况下,实现这种代码兼容性的另一种甚至更好的方法是什么? c> std 在自己的命名空间中,并使用它代替命名空间 std

  • Can please someone clarify to me what exactly is meant by 'undefined behavior' in this case?
  • Is providing C++11/14 compatibility like this considered 'bad practice' even if we just provide types from a newer standard?
  • What would be an alternative or even better way to achieve this kind of code compatibility without having to define everything from namespace std in an own namespace and use this instead of namespace std?

推荐答案


在这种情况下,能否请人向我澄清未定义行为的确切含义?

Can please someone clarify to me what exactly is meant by 'undefined behavior' in this case?

与其他情况相同。该标准不能保证任何行为。

Same as in every other case. The standard doesn't guarantee any behaviour.

程序可能无法编译,成功编译但崩溃,没有崩溃并且具有预期的输出或意外的输出,具体取决于任何变量例如编译器,CPU体系结构或月相。就标准而言。

The program might fail to compile, succesfully compile but crash, not crash and have expected output, or unexpected output depending on any variable such as the compiler, the CPU architechture or the phase of the moon. As far as the standard is concerned.


即使我们只是提供C ++ 11/14兼容性,也被认为是不好的做法。提供新标准中的类型?

Is providing C++11/14 compatibility like this considered 'bad practice' even if we just provide types from a newer standard?

如果我关心程序的标准合规性,而我经常这样做,那么我认为它是坏习惯。

If I care about standard compliance of the program - and I often do, then I consider it a bad practice.


什么是实现这种代码兼容性的另一种甚至更好的方法,而不必在自己的名称空间中定义名称空间std的所有内容并使用它代替命名空间std?

What would be an alternative or even better way to achieve this kind of code compatibility without having to define everything from namespace std in an own namespace and use this instead of namespace std?

您不需要从std定义一切。只需在另一个名称中定义缺少的功能(或使用第三方实现),然后使用std名称空间中的现有功能即可。

You don't need to define everything from std. Simply define the missing features in another namesace (or use a third party implementation), and use the existing features from the std namespace.

这篇关于使用较新的C ++标准的反向移植类型和模板扩展名称空间std的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆