views:

77

answers:

2

I'd like clarification on the C++ standard, specifically where it says (my interpretation) in section 20.1.3 that "for class T and an instance of class T called x, T(x) must be equivalent to x" for the class to work with standard containers.

I couldn't find a definition of 'equivalent'. Does this mean that I have to define operator== as a member of my class, so that T(x) == x returns true?

A: 

This means the class should be copy constructable.
And that the copy constructor creates an object that is equivelent to the original.

If you do not define one the compiler will generate a copy constructor.
If the class does not contain any pointers this should work fine in most situations.

Note: You do not need to define the 'operator =='

Martin York
But what does 'equivalent' mean exactly?
sje397
The standard containers make copies of the object. (they copy the object into the container). As long as you are fine with what is copyied in it is equivalent. For example it shared_pointer copied into a container may not be exactly the same as the original (some internal state may be changed) but it is equivalent to the original.
Martin York
Doesn't that depend on the container? How would a hash table or binary tree work if you couldn't tell if two objects were the same?
Gabe
@Gabe: Some containers need the defintion of operator < (Sutch as Map (ie binary tree)). But that is not what the question about.
Martin York
Can you define 'equivalent' without using the word 'equivalent'? :)
sje397
I am not trying to define it but rather use it in context
Martin York
+1  A: 

Equivalent is purposefully vague. (To avoid things like implying operator== must be defined; it doesn't in a general case.)

However, conceptually two things are equivalent if their data represents the same object. If a class has data that might be different when "copied", then you do need to make an operator== (and possibly operator< along with rel_ops) to make sure that "equivalent" is implemented with respect to that. (Effectively, make sure that the mutable data isn't 'part of the class', so to speak.)

It's usually better not to go such a route, because you end up having to patch lots of things up to make sure it works properly. If something is to be copied, let if be fully copied. This makes much more sense.

GMan
I think your definition is 'right' (hence the accept)...but, it seems extremely odd that the standard (which allows for horribly unreadable code and nested templates that would blow your mind) is putting *conceptual* constraints on my design. For example, if the operator is defined in my design but I haven't typed it yet, and it's not used so would be optimized out anyway, that would be 'compliant'?
sje397
@sje: Heh, I think I see what you're getting at. :) I'd say yes, *in that specific case*. But for a general solution (and hence good advice :P) it should be implemented.
GMan
Also Standard Containers are defined in terms of Concepts and their `value_type` has to be part of a specific set of Concepts. So yes, it is putting conceptual restraints on your classes in the very literal meaning.
pmr