相关疑难解决方法(0)

根据经验确定C++ 11表达式的值类别?

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++ c++11

25
推荐指数
1
解决办法
1007
查看次数

C++临时应该是不变的吗?

我有一个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)

c++ language-lawyer visual-studio-2013 visual-studio-2015

15
推荐指数
2
解决办法
378
查看次数