string foo() { return "hello"; }
int main()
{
//below should be illegal for binding a non-const (lvalue) reference to a rvalue
string& tem = foo();
//below should be the correct one as only const reference can be bind to rvalue(most important const)
const string& constTem = foo();
}
类型的临时对象对 std::string& 类型的非常量引用无效初始化std::stringstd::string 到 std::string & 的转换 非常量引用只能绑定到左值&&,而是在演示代码中,我只是使用非常量左值引用!std::string& from a temporary of type std::stringstd::string to std::string & A non-const
reference may only be bound to an lvalue&&, instead in the demo code, I was just using non-const lvalue reference !这里有人能帮我解释一下VS2010的行为吗?是bug吗!?谢谢
Can somone help me explain the behavior of VS2010 here? Is it a bug !? Thanks
这是 VS 编译器的已知问题/功能.他们一直允许这样做,并且似乎没有任何推动删除该扩展.
That is a known issue/feature of the VS compilers. They have always allowed that and there does not seem to be any push into removing that extension.
这篇关于一个 VS2010 错误?允许在没有警告的情况下绑定非常量引用到右值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么两个函数的地址相同?Why do two functions have the same address?(为什么两个函数的地址相同?)
为什么 std::function 的初始化程序必须是可复制构Why the initializer of std::function has to be CopyConstructible?(为什么 std::function 的初始化程序必须是可复制构造的?)
混合模板与多态性mixing templates with polymorphism(混合模板与多态性)
我什么时候应该使用关键字“typename"?使用模When should I use the keyword quot;typenamequot; when using templates(我什么时候应该使用关键字“typename?使用模板时)
依赖名称解析命名空间 std/标准库Dependent name resolution amp; namespace std / Standard Library(依赖名称解析命名空间 std/标准库)
gcc 可以编译可变参数模板,而 clang 不能gcc can compile a variadic template while clang cannot(gcc 可以编译可变参数模板,而 clang 不能)