获取右值的地址 [英] Getting the address of an rvalue

查看:545
本文介绍了获取右值的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class MyClass {
    public: MyClass(int a) : a(a) { }
    int a;
};

#include <iostream>
void print(MyClass* a) { std::cout << a->a << std::endl; }

int main() {
    print(&static_cast<MyClass&&>(MyClass(1337)));
    return 0;
}

这不适用于GCC 4.6,以前的版本。

This doesn't work with GCC 4.6, while it used to work in a previous version.

现在说:取地址xvalue(rvalue reference)。

Now it says: taking address of xvalue (rvalue reference).

安全地将右值的地址传递给另一个函数?

Is there any way to securely pass the address of an rvalue to another function?

推荐答案


安全地将一个右值引用(临时的地址)传递给另一个函数,而不必将其存储在一个变量中。

is: there is anyway to securely pass an rvalue reference (a.k.a. address of temporary) to another function without boringly storing it in a variable just to do that?

是,如下例所示:

#include <iostream>

class MyClass {
       public: MyClass(int a) : a(a) { }
       int a;
   };


void print(MyClass&& a) { std::cout << a.a << std::endl; }

int main() {
    print( MyClass(1337) );
}

这篇关于获取右值的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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