引用限定符在gcc4.7.2和vc10中给出错误
ref qualifier gives error in gcc4.7.2 and vc10
考虑下面的最小示例:
#include<iostream>
struct A
{
A(){std::cout<<"def"<<'n';}
void foo()&{std::cout<<"called on lvalue"<<'n';}
};
int main()
{
A a;
a.foo();
A().foo();
return 0;
}
给出expecting ';' at the end of declaration
和and expected un-qualified-id before '{'
的错误。
我能知道我做错了什么吗?在实际代码中,我希望避免通过临时调用非静态成员函数。
在GCC 4.7.2和vc2010上试用。谢谢。
答案很简短:VC10和GCC 4.7.2不支持ref-qualifier。
但是,请注意您的foo()
函数有一个左值 ref限定符,这意味着您不能在临时调用它。如果您希望此表达式也可以编译:
A().foo();
那么您应该使用const&
,或者为&&
提供一个重载,就像这个实例一样。
可以使用Clang 3.2或GCC 4.8.1。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 是VC10编译器下的std::common_type错误
- 递归序列化导致vc10/11中出现堆栈溢出错误,但vc9中没有
- 引用限定符在gcc4.7.2和vc10中给出错误