我收到以下错误:
[matt ~] g++ -std=c++11 main.cpp -DCOPY_AND_SWAP && ./a.out
main.cpp: In function ‘int main(int, const char* const*)’:
main.cpp:101:24: error: ambiguous overload for ‘operator=’ in ‘move = std::move<Test&>((* & copy))’
main.cpp:101:24: note: candidates are:
main.cpp:39:7: note: Test& Test::operator=(Test)
main.cpp:52:7: note: Test& Test::operator=(Test&&)
编译以下代码时:
#include <iostream>
#include <unordered_map>
class Test final {
public:
typedef std::unordered_map<std::string, std::string> Map;
public:
Test();
explicit Test(Map&& map);
~Test();
Test(const Test& other);
Test(Test&& test);
#ifdef COPY_AND_SWAP
Test& operator=(Test other);
#else
Test& operator=(const Test& other);
#endif
Test& operator=(Test&& other);
size_t Size() const noexcept;
friend void swap(Test& lhs, Test& rhs);
private:
friend std::ostream& operator<<(std::ostream& stream, const Test& test);
private:
Map map_;
};
Test::Test() : map_() {
std::cerr << "Default constructor called" << std::endl;
};
Test::Test(const Test& other) : map_(other.map_) {
std::cerr << "Copy constructor called" << std::endl;
};
Test::Test(Test&& other) : map_(std::move(other.map_)) {
std::cerr << "Move constructor called" << std::endl;
};
Test::Test(Map&& map) : map_(std::move(map)) {
std::cerr << "Map constructor called" << std::endl;
};
Test::~Test() {};
#ifdef COPY_AND_SWAP
Test& Test::operator=(Test other) {
std::cerr << "Copy and swap assignment called" << std::endl;
using std::swap;
swap(this->map_, other.map_);
return *this;
}
#else
Test& Test::operator=(const Test& other) {
std::cerr << "Copy assignment called" << std::endl;
this->map_ = other.map_;
return *this;
}
#endif
Test& Test::operator=(Test&& other) {
std::cerr << "Move assignment called" << std::endl;
this->map_ = other.map_;
other.map_.clear();
return *this;
}
size_t Test::Size() const noexcept {
return map_.size();
}
void swap(Test& lhs, Test& rhs) {
using std::swap;
swap(lhs.map_, rhs.map_);
}
std::ostream& operator<<(std::ostream& stream, const Test& test) {
return stream << test.map_.size();
}
int main (const int argc, const char * const * const argv) {
using std::swap;
Test::Map map {
{"some", "dummy"},
{"data", "to"},
{"fill", "up"},
{"the", "map"}
};
std::cout << " map size(): " << map.size() << std::endl;
std::cout << "Constructing" << std::endl;
Test test(std::move(map));
std::cout << " map.size(): " << map.size() << std::endl;
std::cout << "test.Size(): " << test.Size() << std::endl;
std::cout << "Copy construction" << std::endl;
Test copy(test);
std::cout << "copy.Size(): " << copy.Size() << std::endl;
std::cout << "Move construction" << std::endl;
Test move(std::move(copy));
std::cout << "move.Size(): " << move.Size() << std::endl;
std::cout << "copy.Size(): " << copy.Size() << std::endl;
std::cout << "Swapping" << std::endl;
swap(move, copy);
std::cout << "move.Size(): " << move.Size() << std::endl;
std::cout << "copy.Size(): " << copy.Size() << std::endl;
std::cout << "Swapping back" << std::endl;
swap(move, copy);
std::cout << "move.Size(): " << move.Size() << std::endl;
std::cout << "copy.Size(): " << copy.Size() << std::endl;
std::cout << "Copy assignment" << std::endl;
copy = test;
std::cout << "test.Size(): " << test.Size() << std::endl;
std::cout << "copy.Size(): " << copy.Size() << std::endl;
std::cout << "Move assignment" << std::endl;
move = std::move(copy);
std::cout << "move.Size(): " << move.Size() << std::endl;
std::cout << "copy.Size(): " << copy.Size() << std::endl;
return 0;
}
当使用 g++ -std=c++11 main.cpp && ./a.out 编译时:
[matt ~] g++ -std=c++11 main.cpp && ./a.out
map size(): 4
Constructing
Map constructor called
map.size(): 0
test.Size(): 4
Copy construction
Copy constructor called
copy.Size(): 4
Move construction
Move constructor called
move.Size(): 4
copy.Size(): 0
Swapping
move.Size(): 0
copy.Size(): 4
Swapping back
move.Size(): 4
copy.Size(): 0
Copy assignment
Copy assignment called
test.Size(): 4
copy.Size(): 4
Move assignment
Move assignment called
move.Size(): 4
copy.Size(): 0
谁能帮我理解为什么在这种情况下使用 copy-and-swap 习语时会出现歧义?
最佳答案
出于重载决议的目的,函数
Test& operator=(Test other);
Test& operator=(Test&& other);
是相等的,因为分别用于转换为 Test 和 Test&& 的隐式转换序列是相等的。前者并不更好,因为直接引用绑定(bind)也算是一种身份转换。
当遇到 2 个同样匹配的歧义时,编译器会报错。你可能想要这个:
#ifdef COPY_AND_SWAP
Test& operator=(Test other);
#else
Test& operator=(const Test& other);
Test& operator=(Test&& other);
#endif
关于c++ - ‘operator=’ 的模糊重载与 c++11 std::move and copy and swap idiom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14503892/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
运行bundleinstall后出现此错误:Gem::Package::FormatError:nometadatafoundin/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p286/cache/libv8-3.11.8.13-x86_64-darwin-12.gemAnerroroccurredwhileinstallinglibv8(3.11.8.13),andBundlercannotcontinue.Makesurethat`geminstalllibv8-v'3.11.8.13'`succeedsbeforebundling.我试试gemin
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我想检查my_number是否在某个范围内,包括较高的值。在IF语句中我会简单地使用“x>100&&x但是我应该在Ruby案例中做什么(开关)?使用:casemy_numberwhenmy_number不起作用。备注:标准范围不包括my_number恰好为500的情况,并且我不想添加第二个“when”,因为我必须编写双重内容casemy_number#between100and500when100..500puts"Correct,dosomething"when500puts"Correct,dosomethingagain"end 最佳答案
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t
我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc