使用继承赋值运算符后,CRTP派生类是否会丢失其成员和段错误?

时间:2017-06-16 14:15:28

标签: c++ templates inheritance crtp

我正在尝试使用4 1/2移动语义规则并使用CRTP从进程中删除重复。这已经证明是困难的,因为尽管编译,当我在使用赋值运算符后尝试访问派生类的成员时,以下代码最终会出现segfaulting。为什么会发生这种情况并且有办法解决这个问题?

基本CRTP类

// BaseCRTP.h

template<class Derived>
class BaseCRTP {
public:
    BaseCRTP() {};

    BaseCRTP(const BaseCRTP &rhs) {
        static_cast<Derived *>(this)->setCore(static_cast<const Derived&>(rhs));
    };

    BaseCRTP(BaseCRTP &&rhs) {
        static_cast<Derived *>(this)->swap(rhs);
    }

    Derived &operator=(BaseCRTP rhs){
        static_cast<Derived *>(this)->swap(rhs);
        Derived& d = *static_cast<Derived*>(this); // debugger shows d now has the correct value for m_member, no issue here
        return *static_cast<Derived*>(this); // something happens here that causes issue? 
    }
};

派生类

// Derived.h

#include "Base.h"
#include <algorithm>

class Derived : public BaseCRTP<Derived>{
private:
    int m_member1;
public:
    using BaseCRTP<Derived>::BaseCRTP;
    using BaseCRTP<Derived>::operator=;

    Derived(int member);
    void setCore(const Derived& rhs);
    void swap(BaseCRTP<Derived> & rhs);
    int getMember() const;
};
  
// Derived.cpp

#include "Derived.h"

void Derived::setCore(const Derived &rhs) {
    m_member1 = rhs.m_member1;
}

void Derived::swap(BaseCRTP<Derived> &rhs) {
    Derived& rhs_p = static_cast<Derived&>(rhs);
    std::swap(m_member1, rhs_p.m_member1); // members have correct values in debugger
}

Derived::Derived(int member) {
    m_member1 = member;
}

int Derived::getMember() const{
    return m_member1;
}

主要

// main.cpp

#include <iostream>
#include "Derived.h"

int main() {
    Derived d(1);
    int z = d.getMember(); // works fine
    Derived dd(34);
    int w = dd.getMember(); // works fine
    d = dd; // after this d and dd no longer have m_member1 values
    int y =  dd.getMember();  //segmentation fault
    int x =  d.getMember();   // when swapped this also segmentation faults
    std::cout << z << w << y << x << std::endl;
    return 0;
}

更新:

我最初更改void swap(BaseCRTP<Derived> & rhs);以使用父类,因为它没有编译而没有这样做,并且调试器似乎表明成员已被维护。我试图在没有运气的情况下将其切换回来,现在的功能是:

void Derived::swap(Derived &rhs) {
    std::swap(m_member1, rhs_p.m_member1);
}

Derived &operator=(BaseCRTP rhs)现在为:Derived &operator=(Derived rhs)

这会导致以下编译时错误:

PATH\main.cpp: In function 'int main()':
PATH\main.cpp:9:9: error: ambiguous overload for 'operator=' (operand types are 'Derived' and 'Derived')
     d = dd;
         ^~
In file included from PATH\Derived.h:7:0,
                 from PATH\main.cpp:2:
PATH\Base.h:14:7: note: candidate: constexpr BaseCRTP<Derived>& BaseCRTP<Derived>::operator=(const BaseCRTP<Derived>&) <deleted>
 class BaseCRTP {
       ^~~~~~~~
PATH\Base.h:28:14: note: candidate: Derived& BaseCRTP<Derived>::operator=(Derived) [with Derived = Derived]
     Derived &operator=(Derived rhs){
              ^~~~~~~~
In file included from PATH\main.cpp:2:0:
PATH\Derived.h:10:7: note: candidate: Derived& Derived::operator=(const Derived&) <deleted>
 class Derived : public BaseCRTP<Derived>{
       ^~~~~~~

显然deleted members still get to participate in overload resolution ......至少可以说有点烦人。肯定有一些方法可以解决这个问题吗?很明显,唯一有效的运算符是operator=我已经定义为唯一的非删除运算符。

更新2

果然,如果我都改变了签名 AND ,那么整个过程就可以了。阻止它成为一个assingment运算符。如果我改为使用以下功能:

Derived& assignmentOperator(Derived rhs){
    static_cast<Derived *>(this)->swap(rhs);
    Derived& d = *static_cast<Derived*>(this); // debugger shows d now has the correct value for m_member, no issue here
    return *static_cast<Derived*>(this); // something happens here that causes issue?
}

并在main.cpp旁边执行:

d.assignmentOperator(dd); 

一切正常,没有编译错误,请参阅所选答案为什么我的原始seg-faulted。我将发布一个新问题,以弄清楚如何解决这些讨厌的语义......

1 个答案:

答案 0 :(得分:2)

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if let lastVisibleIndexPath = tableView.indexPathsForVisibleRows?.last {
        if indexPath == lastVisibleIndexPath {
            // do here...
        }
    }
}

当原始参数的类型为Derived &operator=(BaseCRTP rhs) 时,此赋值运算符将rhs切换到类型BaseCRTP<Derived>。那些成员失去的地方。&#34;

您可以让操作员在Derived中接受BaseCRTP &&而不是using,在那里实施Derived

operator=