包装std::忽略

Wrapping std::ignore

本文关键字:忽略 std 包装      更新时间:2023-10-16

我正在开发一个自定义库。到目前为止,我已经将std::tuple和std::tie打包到我自己的myOwn::tuple和myOwn::tie中。它们的作用与std::tuple和std::tie相同。我想对std::ignore做一些类似的事情。

到目前为止,我已经写了以下内容:

namespace myOwn
{
     auto
     tilde()
     -> decltype(std::ignore)
     {
         return std::ignore;
     }
}

我唯一的问题是,我现在必须使用带括号的myOwn::tilde()。我希望能够将其用作myOwn::波浪号。到目前为止,我在std::ignore上读到的只是如何使用它,

这里:std::ignore 的可能实现

此处:std的要求::忽略

这里:C++:返回std::tie与std::ignore 的类型

我试过使用

typedef std::ignore tilde

但这并不成功。如有任何帮助,我们将不胜感激。这个问题可能对其他试图包装对象的人有用。

如果您不需要修改std::ignore,只想重命名它,那么

decltype(std::ignore) &tilde = std::ignore;

在命名空间范围应该做您需要的事情。

std::ignore不能保证是DefaultConstructibleCopyAssignable,所以它不能被可移植地构建或复制,但我们可以保存对它的引用。这个tilde的类型是decltype(std::ignore) &,但由于std::tie接受引用,所以它的功能应该与std::ignore相同。