开发者

Are . and -> in C and C++ really operators?

开发者 https://www.devze.com 2023-01-27 01:26 出处:网络
you probably have been tought, are tought yourselves, that . and -> are operators which retrieve members of a str开发者_C百科uct (C) or class (C++).

you probably have been tought, are tought yourselves, that . and -> are operators which retrieve members of a str开发者_C百科uct (C) or class (C++).

However, I doubt they are operators - because, if they are operators, what are their input types? Furthermore, the identifiers on both sides are interdependent - a feature which for example the + operator lacks of.

If this is correct - in what sense are these still labeled as operator in practice, and what is their formal definition with regard to language theory.


You assume that the only types which can be passed as arguments to an operator are types that can be defined within the language.

I would argue that any type which can be recognised by the compiler may be passed as an argument, including internal types such as "identifier". The operator will have two arguments in its AST representation, which is enough to allow you to define semantics.

Another argument is that language theory may provide one set of definitions for your vocabulary, but it isn't the only one.

For example, an operator may be a man who works a machine. That definition has no relevance to programming theory, but it won't stop me using for keywords in a domain-specific language expressing something to do with machine operating. Similarly, the term "operator" has a wider definition in mathematics than that which is specific to programming theory - and that definition isn't invalidated simply by working with a programming language.

To put it another way - if you didn't call it an operator, what would you call it?

EDIT

To clarify, my first argument is referring to the syntax for using the operator (the call). These operators have right arguments which are identifiers - member names - which the C++ language cannot express using a data type. The C++ language does have member pointers, but they aren't the same thing as the members - just as a variable isn't the same as a pointer to that variable.

I assume that is what the question referred to. The right parameter of those operators has a type which cannot be expressed or manipulated normally in the language.

What happens when that syntax is mapped to an overloaded operator-> function is a different thing. The function isn't the operator - it's only how the operator gets implemented.


I think the fact that you can overload the -> operator using the "operator" keyword should be a dead giveaway.

Smart pointers do it pretty often:

template<class T>
struct myPtr {
    T *operator ->()    { return m_ptr; }

    private:
        T *m_ptr;
};

The . is not overloadable, but is also an operator by definition.


Hmmm...sizeof is an operator, what is its input type? I don't think the question is useful for distinguishing operators from non-operators in this context.

And that would be because what "operator" means in the context of a programming language is exactly what the author of the language says it means. Shades of Lewis Carroll here.


This reference says they're both operators in C++:

http://www.cplusplus.com/doc/tutorial/operators/

Is that not authoritative enough?


You can overload the -> operator: Wikipedia. That page also states that you can't overload dot. There's an example of -> overloading here:

class String // this is handle
{
  ...
  Stringrep *operator -> () const { return b_; }
  private:
  Stringrep *b_;
}

The arrow works on the value to the left of the arrow and returns whatever the left hand side is "holding inside". Think of a smart pointer.


THe C++03 standard refers to both as operators.

Example:

...after the . operator applied to an expression of the type of its class...

If you are not comfortable with that terminology you can use the term punctuator for ..


Online C standard (n1256):

6.5.2.3 Structure and union members

Constraints

1 The first operand of the . operator shall have a qualified or unqualified structure or union type, and the second operand shall name a member of that type.

2 The first operand of the -> operator shall have type ‘‘pointer to qualified or unqualified structure’’ or ‘‘pointer to qualified or unqualified union’’, and the second operand shall name a member of the type pointed to.

They are operators, and their input types are specified by the standard.


haha, i know people have already said this in a roundabout way but just to say it directly. In C terms, label-> is actually a shorthand for (*label). .That being said, . is the operator which references elements in a struct. Therefore, -> references an element in a pointer to a struct.

0

精彩评论

暂无评论...
验证码 换一张
取 消