我有三个函数调用,我认为应该对它们进行相同的处理,但显然它们不是.我试图理解为什么三个中的一个不编译(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) 如果我写下面的代码:
#include <iostream>
using namespace std;
int main()
{
cout << &(int &&)123 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后g++抱怨:
foo.cc: In function ‘int main()’:
foo.cc:7:20: error: taking address of xvalue (rvalue reference)
Run Code Online (Sandbox Code Playgroud)
好的,多亏了什么是右值,左值,x值,glvalues和prvalues?我得到一个xvalue意味着它即将"过期",这是有道理的.但是现在如果我这样做:
#include <iostream>
using namespace std;
int main()
{
const int &x = (int &&)123;
cout << &x << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这"工作"很好,将打印一个地址.所以,我有几个问题:
通常有一种方法可以知道右值参考的生命周期吗?
像这样的返回类型是否代表c ++ 11中有意义的东西?
template <typename R>
R&& grabStuff();
T instance = grabStuff<T>();
Run Code Online (Sandbox Code Playgroud)
grabStuff如果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&)构造函数可访问,对吗?
你能告诉我代码有什么问题吗? …
一般容器的写入实现的共享内存/副本(如在Qt容器中找到的那样)被C++ 11移动语义和右值引用所取代?
哪一个失败而另一个成功?或者它们是互补的而不是替代品?
我有一个函数,可以通过通用引用接受任何类型,并希望为特定类型重载它(其中一些是自己模板化的,虽然我不认为这在这里很重要).不幸的是,我似乎无法以正确的顺序解决重载问题.
我会假设第二个声明会更受欢迎,foo因为它更具体(模板化程度更低),尽管看起来我对重载决策的理解有些缺乏.有趣的是,将第二个声明更改为X值,使得它打印"好,好"并且X通过非const引用使其打印"坏,好".显然,删除第一个声明会使它返回"好,好",因为没有其他选择.
那么为什么会这样呢?最重要的是,如果以下代码不起作用,如何使用此签名重载函数?
#include <iostream>
#include <string>
class X {};
template<typename T>
inline std::string foo(T && rhs) {
return "bad";
}
inline std::string foo(const X & rhs) {
return "good";
}
int main() {
std::cout << foo(X()) << std::endl;
X x;
std::cout << foo(x) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
也许更迂回的解决方案是间接地做到这一点.摆脱第一种形式foo并使用SFINAE检查是否存在有效的过载,然后它不会调用foo_fallback.
我有一个返回向量或集合的函数:
set<int> foo() {
set<int> bar;
// create and massage bar
return bar;
}
set<int> afoo = foo();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我在函数foo()中创建一个临时内存空间,然后afoo通过复制将其分配给它.我真的想避免这个副本,我可以用C++ 11做到这一点吗?我认为这与rvalue有关.
好的,更新问题:如果我要返回自己定义的对象,而不是向量或设置的东西,这是否意味着我应该定义一个移动构造函数?像这样:
class value_to_return {
value_to_return (value_to_return && other) {
// how to write it here? I think std::move is supposed to be used?
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!!
我有下课
class widget {
// The methods only print their name, i.e. c'tor, destructor etc.
public:
widget();
widget(const widget&);
widget(widget&&);
~widget();
auto operator=(const widget&) -> widget&;
auto operator=(widget&&) -> widget&;
};
Run Code Online (Sandbox Code Playgroud)
我在下面的代码中使用它
#include "widget.h"
auto main() -> int {
widget c(std::move(widget()));
c = std::move(widget());
return 0;
};
Run Code Online (Sandbox Code Playgroud)
由此产生的行为对我来说是可以理解的.在第一次调用中构造一个小部件,然后调用移动构造函数并在临时小部件上调用析构函数.
第二个调用也是这样,期望调用移动赋值运算符而不是移动构造函数.离开main方法,调用析构函数c.
现在有趣的是:
#include "widget.h"
auto main() -> int {
widget c((widget()));
c = widget();
return 0;
};
Run Code Online (Sandbox Code Playgroud)
如果我省略了调用std::move,第一个案例就会停止工作并导致只有一个构造函数调用.而第二个案件仍然像以前一样工作.
我在这里错过了什么?为什么这两个函数调用对待它们的参数有所不同?我在gcc和clang上尝试过这个.
受此评论的启发,关于将lambda与rvalue引用参数直接std::async绑定到,通过std::async编译将rvalue绑定到lambda 并按预期执行:( 实例)
auto lambda = [] (std::string&& message) {
std::cout << message << std::endl;
};
auto future = std::async(lambda, std::string{"hello world"});
future.get();
Run Code Online (Sandbox Code Playgroud)
std::bind但是,使用会触发编译器错误:( 实例)
auto lambda = [] (std::string&& message) {
std::cout << message << std::endl;
};
auto bound = std::bind(lambda, std::string{"hello world"}); // Compiler error
bound();
Run Code Online (Sandbox Code Playgroud)
这是因为std::bind保持message为左值,因此当它将它传递给lambda时,参数不再与参数匹配.
我已经阅读了std::async内部使用的内容std::bind,那么当std::bind它没有时,如何使用rvalue引用参数?是否存在需要此行为的标准的特定部分,或者这取决于编译器?
我想通过引用(广义上,即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++ ×10
rvalue-reference ×10
c++11 ×8
rvalue ×2
containers ×1
g++ ×1
lambda ×1
move ×1
overloading ×1
pointers ×1
reference ×1
stdbind ×1
visual-c++ ×1
xvalue ×1