“后端” nullptr到C ++ - 前C ++ 0x程序 [英] "Backporting" nullptr to C++-pre-C++0x programs

查看:269
本文介绍了“后端” nullptr到C ++ - 前C ++ 0x程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或多或少标题的含义。虽然我尚未使用C ++ 0x 我我喜欢做好准备,当它发生时,我也想减少我必须重写以使用其中的一些设施的代码量。这样我就可以一次得到向后和向前的兼容性。

More or less what the title suggests. While I'm not yet using C++0x I'd like to be prepared for when it happens, and I'd also like to reduce the amount of code I have to rewrite to use some of its facilities. That way I can get backwards and forwards compatibility in one go.

我发现的一个最有趣的是 nullptr ,我最近一直使用更多。

One of the most interesting ones I have found is nullptr, which I've been using more often recently.

检查正式解决方法和 Meyer的建议,我决定我想在我的C +和未来的C + + 0x程序中使用这个。第二部分很简单 - 是一个关键字, nullptr 将被简单地支持。

After checking the "Official workaround" and Meyer's suggestion, I decided that I'd like to use this in both my C++ and future C++0x programs. The second part is simple -- being a keyword, nullptr will simply be supported. But the first part is causing me some discomfort.

Meyers建议的功能如下:

The Meyers proposal functions like this:

class nullptr_t { // ← this is my issue
    // definition of nullptr_t
} nullptr = { };

该提案的问题是它声明该类型被声明为 std :: nullptr_t 根据C ++ 0x的需要。这意味着感觉本机的解决方法,它必须通过重新打开 std :: 命名空间来添加一个类型。 我知道在C ++程序中是非法的(与添加特殊化不同,这显然是皱眉和放手的警告)。

The problem with that proposal is that it declares the type to be declared as std::nullptr_t as required by C++0x. Which means for the workaround to "feel native" it has to be done by reopening the std:: namespace to add a type. I have the understanding that is illegal to do in a C++ program (unlike adding specializations which is apparently frown-and-let-go-with-a-warning).

我想在C ++程序中以舒适的 AND 法律方式使用 nullptr 。我想到的一个选项是在另一个命名空间中声明该类型,然后使用使用

I want to use nullptr in a comfortable AND legal way in a C++ program. One option I had thought of was declaring the type in another namespace and then bring it in using using:

namespace mylibrary {
class nullptr_t {
    ....
} nullptr = { };
// end namespace
}

// These would have to go in the header file.
using mylibrary::nullptr;
using mylibrary::nullptr_t; // apparently this is necessary as well?

这是否正确的方法使它工作?它将强制使用指令,这也强制了 #include 指令的特定顺序。我会不会期望没有预C ++ 0x代码会请求类型 nullptr_t 与命名空间(作为一个函数参数类型,例如)?

Would this be the correct way to make it work? It would force usingdirectives, which also forces a specific order of #include directives as well. Would I be right to expect that no pre-C++0x code would request the type nullptr_t with namespace (as a function argument type, for example)? Would it actually work "feeling native" if it is done this way?

作为补充,这是一个欢迎或皱眉的东西尝试和backport一些漂亮的C ++ 0x的东西到C + +更好的兼容性和编码?在此期间,我已整合此解决方案和其他我正在工作的在一块软件发布

As an addendum, is it a welcomed or frowned upon thing to try and backport some nifty C++0x things to C++ for better compatibility and coding? In the meantime I have integrated this solution and other ones I'm working on in a piece of software to be released.

推荐答案

不允许向添加内容的主要原因namespace std 是你不要混乱已经在那里的东西。否则这可以容易地做到。我在过去发现的多个定义的输出运算符的容器实例化与内置类型像 std :: vector< int> 。但是, nullptr_t 未在此命名空间中定义,添加 typedef 应该是相当无害的。也就是说,我将在与 namespace std 不同的命名空间中定义 nullptr_t ,然后添加 typedef nullptr_t 命名空间std 。变量 nullptr 需要在全局范围内声明,因为它在C ++ 2011中使用不合格。

The main reason you are not allowed to add things to namespace std is so you don't mess things up which are already there. This could otherwise easily be done. Something I found in the past were multiple definitions for output operators for containers instantiated with a built-in type like std::vector<int>. However, nullptr_t isn't defined in this namespace and adding a typedef should be pretty harmless. That is, I would define nullptr_t in a different namespace than namespace std and then add a typedef for nullptr_t to namespace std. The variable nullptr needs to be declared at global scope anyway as it is used unqualified in C++2011.

是否需要类型 std :: nullptr_t 取决于您是否有兴趣添加使用此类型的签名。例如, std :: shared_ptr< T> 可以与 nullptr 进行比较。为此,有必要添加适当的重载,其中提到类型 std :: nullptr_t (或为此类型使用其他名称)。

Whether the type std::nullptr_t is needed depends on whether you are interest in adding the signatures using this type. For example, std::shared_ptr<T> can be compared against nullptr. For this, it is necessary to add suitable overloads which mention the type std::nullptr_t (or use some other name for this type).

这篇关于“后端” nullptr到C ++ - 前C ++ 0x程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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