标签: rvalue-reference

rvalue在C++中的绑定混淆

我有三个函数调用,我认为应该对它们进行相同的处理,但显然它们不是.我试图理解为什么三个中的一个不编译(g ++ -std = c ++ 0x).

// Minimal example to reproduce a compile bug I want to understand.

#include <iostream>
#include <string>

using namespace std;


void bar(const string &&x) { cout << "bar: " << x << endl; }

string returns_a_string() { return string("cow"); }

int main( int argc, char *argv[] )
{
    bar(string("horse"));     // ok
    bar(returns_a_string());  // ok
    string aardvark = "aardvark";
    bar(aardvark);            // not ok, fails to compile, error in next comment
    /*
      rvalue-min.cpp:29:22: error: cannot bind …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue rvalue-reference lvalue-to-rvalue c++11

8
推荐指数
2
解决办法
7848
查看次数

c ++ 11最佳参数传递

考虑这些类:

#include <iostream>
#include <string>

class A
{
    std::string test;
public:
    A (std::string t) : test(std::move(t)) {}
    A (const A & other) { *this = other; }
    A (A && other) { *this = std::move(other); }

    A & operator = (const A & other)
    {
        std::cerr<<"copying A"<<std::endl;
        test = other.test;
        return *this;
    }

    A & operator = (A && other)
    {
        std::cerr<<"move A"<<std::endl;
        test = other.test;
        return *this;
    }
};

class B
{
    A a;
public:   
    B (A && a) …
Run Code Online (Sandbox Code Playgroud)

c++ parameter-passing rvalue-reference c++11

8
推荐指数
1
解决办法
1065
查看次数

为什么对象生成器调用的r值引用需要复制构造函数?

我在使用Visual Studio 2010 C++时遇到以下代码问题.

makeA()只是C++中的一个对象生成器习惯用法(比如std :: make_pair)

#include <stdio.h>

struct A{ // 7th line
    A() {}
    A(A &&) {printf("move\n");}
    ~A() {printf("~A();\n");}
private:
    A(const A &) {printf("copy\n");} // 12th line
};

A makeA()
{
    return A();
}

int main()
{
    A &&rrefA(makeA()); // 22nd line
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误信息

2>d:\test.cpp(22): error C2248: 'A::A' : cannot access private member declared in class 'A'
2>          d:\test.cpp(12) : see declaration of 'A::A'
2>          d:\test.cpp(7) : see declaration of 'A'
2>
Run Code Online (Sandbox Code Playgroud)

我希望makeA()能同时调用A()构造函数和A(A &&)构造函数,并且第22行调用makeA()而不是其他任何东西.(如果没有RVO)编译器不应该要求A(const A&)构造函数可访问,对吗?

你能告诉我代码有什么问题吗? …

c++ visual-studio-2010 rvalue-reference visual-c++ c++11

8
推荐指数
1
解决办法
392
查看次数

共享内存和写入或右值引用上的副本和移动语义?

一般容器的写入实现的共享内存/副本(如在Qt容器中找到的那样)被C++ 11移动语义和右值引用所取代?

哪一个失败而另一个成功?或者它们是互补的而不是替代品?

c++ containers copy-on-write shared-memory rvalue-reference

8
推荐指数
2
解决办法
1207
查看次数

右值对函数的引用

typedef void(&&RF)(void* p);

RF rf()
{
    return f;
}

int ay[10] = { 0 };

typedef int(&&RA)[10];

RA ra()
{
    return ay; // error
}

cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1
Run Code Online (Sandbox Code Playgroud)

C++参考文献说"对函数的rvalue引用被视为左值,无论是否命名".

但我无法理解这是什么考虑因素?我想也许函数的名称总是左值.所以它必须保持左值的属性,并确保将函数名称传递给可以调用的任何地方,比如rf()(NULL).然后数组名称在我脑海中被禁止.我认为它也总是一个左值,所以我编写了上面的代码来测试它并得到一个错误.

谁能指出所有这一切背后的真正原因?

c++ rvalue-reference c++11

8
推荐指数
1
解决办法
831
查看次数

如何获得对右值的引用?

我已经使用std::move和std::forwardC++中.我的问题是:这些功能如何通过标准库实际实现?

如果左值是你可以获得的地址,而右值绝对不是左值,你怎么能实际实现这些引用呢?

这些新设施是否允许以下​​内容:

auto x = &(3); 
Run Code Online (Sandbox Code Playgroud)

或类似的东西?你能获得一个不仅仅是std::move/ forward返回左值的右值的引用吗?

希望这些问题有意义.我无法在Google上找到有关完美转发的教程,等等.

c++ rvalue-reference c++11

8
推荐指数
2
解决办法
2055
查看次数

通过C++中的引用选项传递

我想通过引用(广义上,即A和/或A*)将A类对象(称之为a)传递给另一个B类的构造函数.我不希望在内部修改'a' B('a'是只读的,它的大小很大,这就是我想通过引用传递它的原因).我知道两个选择:

1)将'a'作为

const A & a_
Run Code Online (Sandbox Code Playgroud)

2)将'a'作为

const A * a_
Run Code Online (Sandbox Code Playgroud)

选项1的缺点是我可能错误地传递r值.选项2的缺点是我可能错误地传递空指针.

我的问题是:1)我对上述缺点是否正确?2)是否有另一种推荐的方式通过引用传递'a'?

我在下面的链接中看到了答案.但我很好奇是否有其他选项而不是该问题中的帖子. 在C++中通过引用传递指针是否有好处?

c++ pointers reference rvalue rvalue-reference

8
推荐指数
1
解决办法
266
查看次数

std :: bind和rvalue引用

让我们考虑以下代码:

class Widget{
};

int main(){
Widget w;
auto lambda = bind([](Widget&& ref){ return; }, std::move(w));

return 0;
}
Run Code Online (Sandbox Code Playgroud)

它会触发错误

no match for call to ‘(std::_Bind<main()::<lambda(Widget&&)>(Widget)>) ()’
     lambda();
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么错误出现了?毕竟,我对rvalue引用进行了显式转换 - 我的意思是std::move(w)我通过rvalue引用参数 - 我的意思是Widget&& ref.

这是怎么回事?

而且下面的代码有效,让我更担心的是:

class Widget{
};

int main(){
Widget w;
auto lambda = bind([](Widget& ref){ return; }, std::move(w));

return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

8
推荐指数
1
解决办法
1778
查看次数

以不同方式调度r值和l值并使用sfinae禁用一个选项

我想实现一个功能drop_if.给定一元谓词和顺序容器,它返回一个相同类型的容器,只容纳原始元素中不满足谓词的元素.

如果输入容器是r值,它应该就地工作,否则创建一个副本.这是通过调度到适当的版本来实现的namespace internal.如果value_type容器的容量不能被覆盖(std::pair<const int, int>例如,即使容器是r值),也应该禁用r值版本.

以下代码与clang和当前版本的gcc(> = 6.3)一起按预期工作.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>

namespace internal
{
    template <typename Pred, typename Container,
        typename = typename std::enable_if<
        std::is_assignable<
            typename Container::value_type&,
            typename Container::value_type>::value>::type>
    Container drop_if( Pred pred, Container&& xs )
    {
        std::cout << "r-value" << std::endl;
        xs.erase( std::remove_if( std::begin( xs ), std::end( xs ), pred ), std::end( xs ) );
        return std::move( xs );
    }

    template …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue sfinae rvalue-reference c++11

8
推荐指数
1
解决办法
264
查看次数

将std :: istream &&作为参数是否合理?

我遇到过这样做的代码:

SomeObject parse (std::istream && input) {....
Run Code Online (Sandbox Code Playgroud)

所述input参数是一个rvalue参考,这通常意味着该函数取意参数的所有权.这不是这里发生的事情.

该parse函数将完全消耗输入流,并且它需要rvalue引用,因为然后调用代码将放弃对该属性的所有权,istream因此这是输入流将不可用的信号.

我认为这是可以的,因为由于parse函数实际上没有移动对象,所以没有切掉子类型的危险.从parse观点来看,这基本上表现为普通参考,只有对调用函数有一种可编译的注释,你必须放弃对流的所有权.

这段代码真的安全吗?或者是否有一些被忽视的微妙之处使这变得危险?

c++ iostream istream rvalue-reference

8
推荐指数
2
解决办法
474
查看次数