2

i'm new to c++,

in the link here

it's said

Compare order of types [...] This implementation-specific order is not necessarily related to size, inheritance relations or declaration order, and may differ between programs.

I don't understand the meaning of "order of types", the author shows an example with base type char and int

#include <iostream>   // std::cout
#include <typeinfo>   // operator typeid

int main() {
  if ( typeid(int).before(typeid(char)) )
    std::cout << "int goes before char in this implementation.\n";
  else
    std::cout << "char goes before int in this implementation.\n";

  return 0;
}
Sheed
  • 577
  • 4
  • 18
  • 2
    It just means that the result returned from `.before(...)` is *implementation defined*, so is not portable. Read more [here](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – Steve Lorimer May 14 '17 at 14:28
  • The specific order doesn't matter (and may vary between implementations), just the fact that there is some order. This allows `type_info` instances to be stored in, say, `std::set` (with a suitable comparator). See also [`std::type_index`](http://en.cppreference.com/w/cpp/types/type_index), and in particular its implementation of `operator<` – Igor Tandetnik May 14 '17 at 14:32
  • Still, saying "char comes before int" or "int comes before char", what is it? Is it related to conversion order ? (say `a_float+a_int-> a_float+a_double->a_double+a_double` or `a_float+a_int-> a_double+a_int->a_double+a_double`) function argument call order? etc – Sheed May 14 '17 at 14:44
  • @BenoîtLu No it doesn't have to mean anything... it just helps you to partition types efficiently. – W.F. May 14 '17 at 15:00
  • Sooo c++ introduced a random not-portable order of types with no meaning. Hmm...weird, I guess i'll just accept it. Tks for the answers. – Sheed May 14 '17 at 15:08
  • @BenoîtLu there are cases when reproducible order isn't really necessary... on the other hand without the arbitrary ordering function type partitioning would take O(N^2) while it could be O(N log N). I perfectly understand rationale behind existence of the function and actually find it hard to understand why isn't it constexpr... – W.F. May 14 '17 at 15:20

0 Answers0