Specialization of 'template struct std::less' in different namespace

后端 未结 5 1998
予麋鹿
予麋鹿 2020-12-03 13:42

I am specializing the \'less\' (predicate) for a data type.

The code looks like this:

template<>
struct std::less
{
   bool          


        
5条回答
  •  难免孤独
    2020-12-03 14:07

    Why are you even doing this?

    std::less exists for two purposes only:

    1. to give a name to operator <, allowing it to be passed as a functor
    2. to explicitly allow comparing two pointers that aren't in the same array (which is technically illegal if done with raw pointers)

    There's no reason for a user to overload it - either overload operator< or use a custom comparator function.

    There are std algorithms that can be sensibly specialized - std::swap is a good example - and to do so you do need to declare the specialization inside namespace std.

提交回复
热议问题