【发布时间】:2014-07-11 05:35:59
【问题描述】:
写。建议的重复"Is pass-by-value a reasonable default in C++11?" - 那里的问题也没有那里的答案没有提到“通用参考”构造函数版本,所以I 真的看不到重复。考虑重新开放。
我正在熟悉移动语义,正在尝试它。请看一下这段(可编译的)代码:
#include <iostream>
#include <string>
struct my_str {
std::string s;
my_str(const std::string & str): s(str) { std::cout << " my_str parameter ctor" << std::endl; }
my_str(const my_str & o): s(o.s) { std::cout << " my_str copy ctor" << std::endl; }
my_str(my_str && o): s(std::move(o.s)) { std::cout << " my_str move ctor" << std::endl; }
};
template <typename T>
my_str build_ur(T && s) {
return my_str(std::forward<T>(s));
}
my_str build_val(my_str s) {
return my_str(std::move(s));
}
int main() {
my_str s1("hello");
my_str s2("world");
std::cout << "Building from universal reference (copy):" << std::endl;
build_ur(s1);
std::cout << "Building from universal reference (move):" << std::endl;
build_ur(std::move(s1));
std::cout << "Building from value (copy):" << std::endl;
build_val(s2);
std::cout << "Building from value (move):" << std::endl;
build_val(std::move(s2));
std::cout << std::endl;
return 0;
}
输出:
g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
my_str parameter ctor
my_str parameter ctor
Building from universal reference (copy):
my_str copy ctor
Building from universal reference (move):
my_str move ctor
Building from value (copy):
my_str copy ctor
my_str move ctor
Building from value (move):
my_str move ctor
my_str move ctor
http://coliru.stacked-crooked.com/a/3be77626b7ca6f2c
这两个函数在这两种情况下都做得很好。按值函数再次调用移动构造函数,但这应该很便宜。您能否评论一下哪种模式应该优先于另一种模式的情况?
【问题讨论】:
-
我会选择 my_str build(T && s),因为 my_str build(my_str s) 将移动局部函数变量 s(传递给函数时将被复制构造)跨度>
-
你用的是什么编译器?第一个版本绝对应该使用复制ctor。 ideone.com/L8ZrI5
-
@Dario:嗯,你在使用一些优化吗?我实际上用我的 gcc 4.8.1 看到了一个 ctor
-
@Exceptyon 我更新了代码,现在我的意图应该更清楚了。
-
@Jarod42:你错了。移出对象位于"unspecified but valid state" 中。这不是 UB。
标签: c++ c++11 move forwarding-reference