如何使用对象的引用来初始化 boost::any ?

发布于 2024-12-26 15:16:32 字数 585 浏览 4 评论 0原文

我想在 boost::any 对象中存储对对象的引用。如何初始化 boost::any 对象?我尝试了 std::ref(),但是 boost::any 是用 std::reference_wrapper 初始化的。例如,以下

#include <boost/any.hpp>
#include <cxxabi.h>
#include <iostream>

int main(void)
{
  int s;
  int i = 0;
  boost::any x(std::ref(i));
  std::cout << abi::__cxa_demangle(x.type().name(), 0, 0, &s) << "\n";
  return 0;
}

打印

std::reference_wrapper<int>

我希望 boost::any 包含 int&

I want to store a reference to an object in a boost::any object. How do I initialize the boost::any object? I tried std::ref(), but boost::any gets initialized with std::reference_wrapper<>. For example, the following

#include <boost/any.hpp>
#include <cxxabi.h>
#include <iostream>

int main(void)
{
  int s;
  int i = 0;
  boost::any x(std::ref(i));
  std::cout << abi::__cxa_demangle(x.type().name(), 0, 0, &s) << "\n";
  return 0;
}

prints

std::reference_wrapper<int>

I want the boost::any to contain int& instead.

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

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

发布评论

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

评论(2

他是夢罘是命 2025-01-02 15:16:32

boost::any 类没有允许这样的接口:您需要使用构造函数指定引用的类型。我认为您不能显式指定模板化构造函数的类型,因为我看不到任何可以粘贴它的地方。即使您可以显式指定模板参数,它在 C++2003 中也不起作用,因为没有可用的引用折叠,并且该参数被声明为采用 T const&:您会试图创建一个T& const& 不会飞。
我认为你最好的选择是如果你坚持要像远程引用一样使用 std::reference_wrapper ,或者只是使用 T*

也就是说,通常可以有一个类似于 boost::any 类型的模板化静态因子方法,用于显式指定模板参数。然而,由于 boost::any 是故意设计来处理值类型的,所以并没有这样做。我有点怀疑是否也应该这样做:使用指针是非常好的选择。如果您确实需要引用类型,您可能必须自己实现它。

The boost::any class doesn't have an interface allowing something like this: you would need to specify the type of the reference with the constructor. I don't think that you can explicitly specify the type of templated constructor because I don't see any place you could stick it. Even if you can explicitly specify the template parameter, it wouldn't work in C++2003 bcause there is no reference collapsing available and the parameter is declared as taking a T const&: you'd be trying to create a T& const& which won't fly.
I think your best option is to either use std::reference_wrapper<T> if you insist on something looking remotely reference like or just to use T*.

That said, it would be generally possible to have a templatized static factor method of a type similar to boost::any which would be used to explicitly specify the template argument. However, since boost::any is deliberately designed to deal with value types this isn't done. I'm a bit dubious whether it should be done as well: using a pointer is perfectly good alternative. If you really need a reference type you'll probably have to implement it yourself.

囍孤女 2025-01-02 15:16:32

该行为是正确的、预期的和适当的。 std::ref 是一个辅助函数,用于创建 std::reference_wrapper 类型的对象,引用包装器是一个具有值语义的类,用于保存引用-- 如果您希望容器跟踪外部引用,那么这正是您想要放入容器中的东西。

因此,只需采用您拥有的解决方案即可。

如果愿意的话,您不能拥有直接、裸引用的容器,就像不能拥有引用数组一样。包装纸的设计正是为了满足这种需求。

The behaviour is correct, expected and appropriate. std::ref is a helper function that creates an object of type std::reference_wrapper<T>, and the reference wrapper is a class with value semantics that holds a reference -- that's exactly the sort of thing you want to put into a container if you want the container to track outside references.

So just go with the solution you have.

If you will, you cannot have a container of direct, naked references, much like you cannot have an array of references. The wrapper is designed precisely to accommodate such needs.

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