1

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?

sje397
  • 41,293
  • 8
  • 87
  • 103
  • I'd appreciate a proper link to the standards doc so I can quote it properly, if anyone has one handy :) – sje397 Jul 19 '10 at 05:15
  • Unfortunately it's not free: http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents – GManNickG Jul 19 '10 at 05:17

2 Answers2

1

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 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 Jul 19 '10 at 05:45
  • @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. – GManNickG Jul 19 '10 at 05:52
  • 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 Jul 19 '10 at 06:03
0

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
  • 257,169
  • 86
  • 333
  • 562
  • But what does 'equivalent' mean exactly? – sje397 Jul 19 '10 at 05:19
  • 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 Jul 19 '10 at 05:21
  • 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 Jul 19 '10 at 05:41
  • @Gabe: Some containers need the defintion of operator < (Sutch as Map (ie binary tree)). But that is not what the question about. – Martin York Jul 19 '10 at 05:48
  • Can you define 'equivalent' without using the word 'equivalent'? :) – sje397 Jul 20 '10 at 07:57
  • I am not trying to define it but rather use it in context – Martin York Jul 20 '10 at 08:04