onqtam
onqtam

Reputation: 4528

how to avoid many similar overloads for C strings

Here is the code:

template <typename L, typename R> bool eq (const L& lhs, const R& rhs) { return lhs == rhs; }

template<int N> bool eq(char* lhs,           const char(&rhs)[N]) { return String(lhs).compare(rhs) == 0; }
template<int N> bool eq(const char(&lhs)[N], char* rhs)           { return String(lhs).compare(rhs) == 0; }
inline          bool eq(char* lhs,           char* rhs)           { return String(lhs).compare(rhs) == 0; }
inline          bool eq(const char* lhs,     const char* rhs)     { return String(lhs).compare(rhs) == 0; }
inline          bool eq(char* lhs,           const char* rhs)     { return String(lhs).compare(rhs) == 0; }
inline          bool eq(const char* lhs,     char*  rhs)          { return String(lhs).compare(rhs) == 0; }

I have to do this for neq/lt/gt/lte/gte and not just for equality. Maybe I've already missed something.

Is there a way to not list all the possible combinations of C string types?

Also C++98.

EDIT: >> here << is an online demo with the problem

Upvotes: 0

Views: 84

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275500

namespace details{
  template<template<class...>class Z,class,class...Ts>
  struct can_apply:std::false_type{};
  template<template<class...>class Z,class...Ts>
  struct can_apply<Z,std::void_t<Z<Ts...>>,Ts...>:std::true_type{};
}
template<template<class...>class Z,class...Ts>
using can_apply=details::can_apply<Z,void,Ts...>;

This tests if a template can be applied some types.

namespace strcmp{
  bool eq(const char*lhs, const char*rhs){/* body */}
}
template<class L, class R>
using str_eq_r=decltype(strcmp::eq(std::declval<L>(),std::declval<R>()));

template<class L, class R>
using can_str_eq=can_apply<str_eq_r,L,R>;

can_str_eq is truthy iff we can call stdcmp::eq on it.

namespace details {
  bool eq(const char* lhs, const char* rhs, std::true_type){
    return strcmp::eq(lhs,rhs);
  }
  template<class L,class R>
  bool eq(L const& l, R const&r,std::false_type){
    return l==r;
  }
}
template<class L,class R>
bool eq(L const& l, R const&r){
  return details::eq(l,r,can_str_eq<L const&,R const&>{});;
}

We could also use a static_if trick to do it inline, if you like:

template<class L,class R>
bool eq(L const& l, R const&r){
  return static_if<can_str_eq>( l, r )(
    strcmp::eq,
    [](auto&& l, auto&& r){return l==r;}
  );
}

After writing a static_if:

template<class...Ts>
auto functor(Ts...ts){
  return [=](auto&& f){
    return f(ts...);
  };
}
namespace details{
  template<class Functor>
  auto switcher(std::true_type, Functor functor){
    return [=](auto&& t, auto&&){
      return functor(t);
    };
  }

  template<class Functor>
  auto switcher(std::false_type, Functor functor){
    return [=](auto&&, auto&& f){
      return functor(f);
    };
  }
}

template<template<class...>class test, class...Ts>
auto static_if(Ts...ts){
  return details::switcher(
    test<Ts...>{},
    functor(ts...)
  );
}

now, what are the odds that works? (Written on phone, not compiled yet) Also not optimal: lots of perfect forwarding, some of which requires de-lamdaing, required.

Upvotes: 1

T.C.
T.C.

Reputation: 137315

Decay an array type to pointer:

template<class T>
struct decay_array { typedef T type; };
template<class T, size_t N>
struct decay_array<T[N]> { typedef T* type; };
template<class T>
struct decay_array<T[]> { typedef T* type; };

Check that a type is not a pointer to (possibly const) char:

template<class T>
struct not_char_pointer { enum { value = true }; };
template<>
struct not_char_pointer<char*> { enum { value = false }; };
template<>
struct not_char_pointer<const char*> { enum { value = false }; };

Now check that a type is not a pointer to or array of (possibly const) char:

template<class T>
struct can_use_op : not_char_pointer<typename decay_array<T>::type> {};

Reimplement std::enable_if:

template<bool, class = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };

and use it to constrain your template:

template <typename L, typename R> 
typename enable_if<can_use_op<L>::value || can_use_op<R>::value, bool>::type 
eq (const L& lhs, const R& rhs) { return lhs == rhs; }

Then just one overload is enough:

inline bool eq(const char* lhs, const char* rhs) { return String(lhs).compare(rhs) == 0; }

Upvotes: 4

Related Questions