Home >Backend Development >C++ >Does Overloading Operators Eliminate Undefined Behavior in Expressions like `i = i` with User-Defined Types?

Does Overloading Operators Eliminate Undefined Behavior in Expressions like `i = i` with User-Defined Types?

DDD
DDDOriginal
2024-12-22 19:27:13893browse

Does Overloading Operators Eliminate Undefined Behavior in Expressions like `i  =   i` with User-Defined Types?

Undefined Behavior and Sequence Points Revisited

The expression i = i has been labeled as invoking undefined behavior when i is a built-in type. However, it raises questions about the behavior of similar expressions when i is a user-defined type.

User-Defined Type Considerations

The type Index is defined as:

class Index {
    int state;

    public:
        Index(int s) : state(s) {}
        Index& operator++() {
            state++;
            return *this;
        }
        Index& operator+=(const Index &index) {
            state += index.state;
            return *this;
        }
        operator int() {
            return state;
        }
        Index& add(const Index &index) {
            state += index.state;
            return *this;
        }
        Index& inc() {
            state++;
            return *this;
        }
};

Would expressions like i.operator =(i.operator ()); or i.add(i.inc()); still invoke undefined behavior?

Sequence Point Implications

The expression i.operator =(i.operator ()); does not invoke undefined behavior because:

  • Overloaded operators are functions, which have sequence points before and after their evaluation.
  • The operator () and operator =() calls thus have well-defined sequence points.
  • Therefore, the expression behaves as expected without violating sequence point rules.

Non-Expression Considerations

The original expression i = i raises the question of whether it is an expression at all. If it is not, it may not be subject to sequence point rules. However, this argument is unlikely to hold because it is parsed and executed as an expression.

Multiple Modifications

The other expression of interest, a[ i] = i, also raises questions due to the potential modifications between sequence points. However, it is well-defined when a is a user-defined type that overloads the subscript operator.

Expression Complexity

The expression i is well-defined in C 03 because it is equivalent to ((i.operator ()).operator ()).operator (), which has well-defined sequence points between function evaluations.

Conclusion

The behavior of expressions involving user-defined types and sequence points depends on the specific implementation of the operators. Overloaded operators behave as functions, providing sequence points. However, whether an expression is subject to sequence point rules may depend on its syntactic structure and interpretation as an expression.

The above is the detailed content of Does Overloading Operators Eliminate Undefined Behavior in Expressions like `i = i` with User-Defined Types?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn