C++ 11中的每个表达式都有一个值类别.lvalue,xvalue或prvalue之一.
有没有办法编写一个宏,给定任何表达式作为参数,将产生一个字符串"lvalue","xvalue"或"prvalue"酌情?
例如:
int main()
{
int x;
cout << VALUE_CAT(x) << endl; // prints lvalue
cout << VALUE_CAT(move(x)) << endl; // prints xvalue
cout << VALUE_CAT(42) << endl; // prints prvalue
}
Run Code Online (Sandbox Code Playgroud)
怎么可以VALUE_CAT实现?
我有一个C++类,它具有以下接口:
class F {
public:
F(int n, int d);
// no other constructors/assignment constructors defined
F& operator *= (const F&);
F& operator *= (int);
int n() const;
int d() const;
};
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
const F a{3, 7};
const F b{5, 10};
auto result = F{a} *= b; // How does this compile?
Run Code Online (Sandbox Code Playgroud)
在Visual Studio(VS)2013下,注释行编译时没有错误.在VS2015下,产生错误C2678:
error C2678: binary '*=': no operator found
which takes a left-hand operand of type 'const F'
(or there is no acceptable conversion)
note: could be 'F &F::operator …Run Code Online (Sandbox Code Playgroud)