C ++ - 如何通过引用返回prvalue? [英] C++ - how to return a prvalue by reference?

查看:108
本文介绍了C ++ - 如何通过引用返回prvalue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我实现一个本机数组包装器,它将允许这样作为函数参数传递和返回。我有一个麻烦,但是将它转换为本机数组,因为本机数组不能返回。作为替代,我决定使用rvalue引用返回类型的转换操作符,但是这将无法正确,因为如果我想绑定返回的对象到一个'rvalue'引用为了延长它的生命时间这不会发生,因为它是一个xvalue而不是prvalue。有问题的解决方案吗?也许一些prvalue演员?

So I'm implementing a native arrays wrapper which will allow such to be passed as function arguments and to be returned. I'm having a trouble however with casting it to a native array as native arrays can't be returned. As an replacement I decided to use 'rvalue' reference return type of the casting operator but this will not act correctly because if I want to bind the returned object into an 'rvalue' reference in order to extend it's life-time this won't happen as it's an 'xvalue' and not 'prvalue'. Is there any solution for the problem? Maybe some 'prvalue' cast? Or if there is some other way to implement this implicit cast to 'array'?

类:

template<typename type>
struct tmp
{
    tmp() {}
    tmp(const tmp &) = default;
    tmp(const type & arg) : tmp(*(const tmp*)arg) {}

    && operator type() && {return static_cast<type&&>(d);}

    ~tmp () { cout << "tmp destructor" << endl; }

    type d;

};

使用它的代码:

tmp<tmp<int [4]>> Func() // used 'tmp<int [4]>' instead of array to track object destruction (but normally it should be an native array type
{
    return tmp<tmp<int [4]>>();
}

int main()
{
    tmp<int [4]> &&tmp1 = Func(); //implicit cast (from 'tmp<tmp<int [4]>>') to 'tmp<int [4]>', calls tmp::operator type()

    cout << "Here" << endl;

    return 0;
}

程序输出:


tmp析构

tmp destructor

tmp destructor

tmp destructor

这里

生活 example

推荐答案

prvalue是一个不是xvalue的值,也称为临时对象或子对象

A prvalue is an rvalue that is not an xvalue, aka "a temporary object or subobject thereof, or a value that is not associated with an object."

您不能创建一个临时对象(12.2)的数组,也不能创建与对象不相关联的数组值

You cannot create an array that is a a temporary object (12.2), nor can you create an array value that is not associated with an object.

对于一个数组是一个prvalue,它留下一个子对象临时对象。

For an array to be a prvalue, that leaves a subobject thereof of a temporary object.

c $ c> tmp :

So a tmp:

template<typename type>
struct tmp
{
  tmp() {}
  tmp(const tmp &) = default;
  tmp(tmp &&) = default;
  tmp(const tmp &&o):tmp(o) {}
  tmp(tmp &o):tmp(const_cast<tmp const&>(o)){}

  template<class... Ts>
  tmp(Ts&&...ts) : v{std::forward<Ts>(ts)...} {}

  ~tmp () { std::cout << "tmp destructor\n"; }

  type v;
};

A wrap_as_tmp

template<class X, class... Ts>
tmp<X> wrap_as_tmp(Ts&&... ts)
{
  return {std::forward<Ts>(ts)...};
}

以跟踪销毁我们使用 noisy

struct noisy {
  ~noisy() { std::cout << "bang\n"; }
};

然后测试:

int main() {
  auto&& x = wrap_as_tmp<noisy[4]>().v;
  std::cout << "There\n";
}

并注意 noisy 对象爆炸之前的输出。

and note that There outputs before the noisy objects explode.

live example

请注意最后使用 .v

如果你的目标是避免这种情况,那么你不能。

If your goal is to avoid that, too bad, you cannot.

这篇关于C ++ - 如何通过引用返回prvalue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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