Does the order in the specialization of the function in the class template matter

Consider something like ...

template<typename T>
class Vector {
  ...
  bool operator==( const Vector<float> &rhs ) {
    // compare and return
  }

  bool operator==( const Vector<T> &rhs ) {
    // compare and return
  }
  ...
 };

Please note that specialization is above the non-specialized version. If I put the specialized version below the non-specialized version, would the comparison Vector<float>== still work as intended? For some reason, I think I remember reading that if you put the specialization below in this scenario, then when the compiler looks at the header, it will see by default, see that it works, and use it.

+3
source share
2 answers

, , . ==, . , .

, . ,

template <typename T>
void Foo(Vector<T> x) {
   std::cout << "x" << std::endl;
}
template <>
void Foo(Vector<float> y) {
   std::cout << "y" << std::endl;
}

...
Vector<int> intVec;
Vector<float> floatVec;
Foo(intVec); //prints x
Foo(floatVec); //prints y

, Foo Vector<float>, .

- . , , . . , . , . Failure (SFINAE). http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error .

-3

, . ( ), , ++. , , , - . .

- , , , , , , , , , , .

, - , ​​ , , , ; .

, , - , , - , - , - , - , - - , , -- - .., , - - , .. , , . ; , , .

+6

Source: https://habr.com/ru/post/1760835/


All Articles