简体   繁体   English

在 C++ 中取消引用这个指针是否有效?

[英]Is it valid to dereference this pointer in C++?

struct myclass{
    static const int invalid = -1;
    /*explicit*/ myclass(int i, double d = 0.0){
        _var = i
    }
    int _var; 
    bool operator < (const myclass& rhs);
    bool operator > (const myclass& rhs);
    bool operator == (const myclass& rhs);
    bool operator != (const myclass& rhs);
    /*
    bool operator == (int rhs){
        return *(this) == myclass(rhs); // Is this valid C++ ?
    }
    bool operator != (int rhs){
        return *(this) == myclass(rhs); // Is this valid C++ ?
    }
    */
};

int userCodeCall() {
    myclass m(10);
    
    // Valid use Case
    if(m != myclass::invalid) {
        //.... do something
    }
    
    // Invalid use case
    if(m < 0.5) { // I want this to give me a compiler error
        //.... do something
    }
    // Invalid use case
    if(m < 5) { // I want this to give me a compiler error
        //.... do something
    }
}

I am working on a legacy codebase and I came across a class that can be implicitly constructed from int.我正在处理遗留代码库,我遇到了一个可以从 int 隐式构造的类。 And I found a bug where we were performing a < than comparison with a double.我发现了一个错误,我们在执行 < than 与双精度比较时。 I thought of fixing this using an explicit with the constructor and then separately defining a == operation with an integer.我想通过构造函数使用显式来解决这个问题,然后用一个整数单独定义一个 == 操作。 But, I am not comfortable with the design.但是,我对设计不满意。 As I haven't seen this pattern anywhere else.因为我在其他任何地方都没有看到这种模式。

bool operator == (int rhs){
    return *(this) == myclass(rhs); // Is this valid C++ ?
}

I have two questions?我有两个问题?

  1. Is it valid C++11/14/17? C++11/14/17 是否有效?

  2. Can the design of the class be improved upon?类的设计可以改进吗? Given I still want to have a "==" comparison style usage to stay valid as it is all over the user codebase.鉴于我仍然希望使用“==”比较样式来保持有效,因为它遍布用户代码库。

     if(m != myclass::invalid) // some similar API need to be supported, if not the same.

The solution you have is fine, but it seems you just want to prevent specific comparison operators from working when numeric types are on the right hand side.您拥有的解决方案很好,但是当数字类型位于右侧时,您似乎只想阻止特定的比较运算符工作。 You can do that by simply deleting those specific operators, for all types other than myclass .对于除myclass之外的所有类型,您可以通过简单地删除那些特定的运算符来做到这一点。

template<typename T>
bool operator<(T) = delete;
    
template<typename T>
bool operator>(T) = delete;

The more common idiom for this kind of validity testing is to use an explicit operator bool:这种有效性测试更常见的习惯用法是使用显式运算符 bool:

struct myclass {
 private:
    static const int _invalid = -1;
    int _var;
 public:
    explicit myclass(int i, double d = 0.0){
        _var = i
    } 
    explicit operator bool() const { return _var != _invalid; }
    bool operator < (const myclass& rhs);
    bool operator == (const myclass& rhs);
    bool operator != (const myclass& rhs);
    bool operator > (const myclass& rhs);
};

int userCodeCall() {
    myclass m(10);
    
    // Valid use Case
    if(m) {
        //.... do something
    }
    
    // Invalid use case
    if(m < 0.5) { // gives a compile error
        //.... do something
    }
    // Invalid use case
    if(m < 5) { // gives a compile error
        //.... do something
    }
}

If this is too much and you must have a myclass::invalid to test against, you can just make it an instance of the class:如果这太多并且您必须有一个 myclass::invalid 来测试,您可以将其设为类的一个实例:

static constexpr myclass invalid(-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM