有没有更简单的方法来实现这个功能的效果?
strong_ordering reverse(strong_ordering v) {
if (v > 0)
return strong_ordering::less;
else if (v < 0)
return strong_ordering::greater;
else
return v;
}
Run Code Online (Sandbox Code Playgroud) 我试图擦除存储包含const成员的类的向量的元素,但失败并出现错误。
当我删除常量修饰符时,程序将正常编译。
class C
{
public:
const int i;
C(int i = 1):i(i){};
};
int main()
{
vector<C> v;
C c;
v.push_back(c);
v.erase(v.begin());
return 0;
}
error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function
m.cpp(151): note: compiler has generated 'C::operator =' here
m.cpp(151): note: 'C &C::operator =(const C &)': function was implicitly deleted because 'C' has a data member 'C::i' of const-qualified non-class type
m.cpp(146): note: see declaration of 'C::i'
Run Code Online (Sandbox Code Playgroud) 像这样的对象:
#include <vector>
using namespace std;
class A
{
public:
vector<int> i;
//save other variable
};
void save(const A & a)
{
//dosomething
}
int main()
{
A a;
save(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何将其保存到二进制文件?换句话说,如何编写保存功能?
与 cv 限定不同,ref 限定不会更改指针的属性
this:在右值 ref 限定函数内,*this仍然是左值表达式。
那么,我们如何获得 的 ref-qualifier *this,或者如何转发*this,以避免重复使用不同的 ref-qualifier 实现相同的成员函数呢?