【发布时间】:2016-10-24 11:01:29
【问题描述】:
所以我问了this question,我正在修补通过static_cast 解决它。 (顺便说一句,它确实解决了问题,我只是不确定我是否理解为什么。)
在代码中:
vector<int> foo = {0, 42, 0, 42, 0, 42};
replace(begin(foo), end(foo), static_cast<int>(foo.front()), 13);
static_cast 是否只是构造一个 R 值 int?那和只是调用有什么区别:
replace(begin(foo), end(foo), int{foo.front()}, 13);
编辑:
正如static_cast的答案所推断的那样确实似乎构造了一个R值int:http://ideone.com/dVPIhD
但此代码不在 Visual Studio 2015 上工作。这是编译器错误吗?在这里测试:http://webcompiler.cloudapp.net/
【问题讨论】:
-
我没有任何标准的引用来支持我,但我认为您可以使用
+foo.front()来获取值的副本,而不是对符号的引用
标签: c++ casting reference rvalue static-cast