“向后移植” nullptr 到 C++-pre-C++0x 程序

发布于 2024-12-24 23:50:35 字数 1582 浏览 2 评论 0原文

或多或少如标题所暗示的那样。虽然我尚未使用C++0x我我希望在这种情况发生时做好准备,并且我还希望减少为使用其某些功能而必须重写的代码量。这样我就可以一次性获得向后和向前兼容性。

我发现的最有趣的之一是 nullptr,我最近使用得比较频繁。

在检查了“官方解决方法”和Meyer的建议后,我决定我想在我的 C++ 和未来的 C++0x 程序中使用它。第二部分很简单——作为关键字,nullptr 将被简单地支持。但第一部分让我有些不舒服。

Meyers 提案的功能如下:

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

该提案的问题在于,它按照 C++0x 的要求声明了要声明为 std::nullptr_t 的类型。这意味着要让解决方法“感觉原生”,必须通过重新打开 std:: 命名空间来添加类型来完成。 我知道在 C++ 程序中这样做是非法的(与添加专业化不同,这显然是皱眉并警告)。

我想在 C++ 程序中以舒适的AND合法方式使用nullptr。我想到的一个选择是在另一个名称空间中声明类型,然后使用 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?

这是使其工作的正确方法吗?它将强制 using 指令,这也强制 #include 指令的特定顺序。我是否正确地期望 C++0x 之前的代码不会请求带有命名空间的类型 nullptr_t (例如,作为函数参数类型)?如果这样做的话,它真的会“感觉原生”吗?


作为附录,为了更好的兼容性和编码而尝试将一些漂亮的 C++0x 东西反向移植到 C++ 是受欢迎还是令人皱眉?与此同时,我已将这个解决方案和我正在开发的其他解决方案集成到一个软件中被释放。

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.

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

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.

The Meyers proposal functions like this:

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

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).

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?

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?


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-12-31 23:50:35

除非您能想到需要声明 nullptr_t 类型的另一个对象(我不能),否则我会隐藏该类型并放置 nullptr< /code> 在全局命名空间中尽可能模仿关键字:

namespace mylibrary
{
    class nullptr_t
    {
        ....
    };
}

mylibrary::nullptr_t nullptr = { };

Unless you can think of a reason you need to declare another object of type nullptr_t (I can't), I would hide the type away and put nullptr in the global namespace to mimic a keyword as closely as possible:

namespace mylibrary
{
    class nullptr_t
    {
        ....
    };
}

mylibrary::nullptr_t nullptr = { };
无声无音无过去 2024-12-31 23:50:35

不允许您向 namespace std 添加内容的主要原因是这样您就不会弄乱已经存在的内容。否则这可以很容易地完成。我过去发现的是使用 std::vector 等内置类型实例化的容器的输出运算符的多个定义。然而,nullptr_t 并未在此命名空间中定义,因此添加 typedef 应该是无害的。也就是说,我将在与 namespace std 不同的命名空间中定义 nullptr_t,然后为 nullptr_t 添加一个 typedef命名空间 std。无论如何,变量 nullptr 都需要在全局范围内声明,因为它在 C++2011 中以不限定的方式使用。

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

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文