31

I find the C++ STL method of doing simple set operations quite clunky to use. For example, to find the difference between two sets:

std::set<int> newUserIds;
set_difference(currentUserIds.begin(), currentUserIds.end(), mPreviousUserIds.begin(), mPreviousUserIds.end(), std::inserter(newUserIds, newUserIds.end()));
std::set<int> missingUserIds;
set_difference(mPreviousUserIds.begin(), mPreviousUserIds.end(), currentUserIds.begin(), currentUserIds.end(), std::inserter(missingUserIds, missingUserIds.end()));
mPreviousUserIds = currentUserIds;

Does boost offer an alternative set of classes that would reduce the above example to something like this:

set_type<int> newUserIds = currentUserIds.difference(mPreviousUserIds);
set_type<int> missingUserIds = mPreviousUserIds.difference(currentUserIds);

(Similar to QSet in Qt which overrides operator- in this way.)

2
  • 5
    It's a five-finger exercise to write that if you want it. Feb 26, 2013 at 14:02
  • 2
    It is but it leaves me with a personal base of code to import into any project where I need it, which isn't always possible (e.g. at work) and makes it harder for others to understand the code.
    – Tim MB
    Feb 26, 2013 at 14:41

3 Answers 3

93

Nope. But I here is how to clean it up.

First, rewrite iterator based functions as ranged based functions. This halves your boilerplate.

Second, have them return container builders rather than take insert iterators: this gives you efficient assignment syntax.

Third, and probably too far, write them as named operators.

The final result is you get:

set<int> s = a *intersect* b;
set<int> s2 = c -difference- s;
set<int> s3 = a *_union_* (b *intersect* s -difference- s2);

... after writing a boatload of boilerplate code elsewhere.

As far as I know, boost does step 1.

But each of the above three stages should reduce your boilerplate significantly.

Container builder:

template<typename Functor>
struct container_builder {
  Functor f;
  template<typename Container, typename=typename std::enable_if<back_insertable<Container>::value>::type>
  operator Container() const {
    Container retval;
    using std::back_inserter;
    f( back_inserter(retval) );
    return retval;
  }
  container_builder(Functor const& f_):f(f_) {}
};

which requires writing is_back_insertable (pretty standard SFINAE).

You wrap your ranged based (or iterator based) functor that takes a back_insert_iterator as the last argument, and use std::bind to bind the input parameters leaving the last one free. Then pass that to container_builder, and return it.

container_builder can then be implicitly cast to any container that accepts std::back_inserter (or has its own ADL back_inserter), and move semantics on every std container makes the construct-then-return quite efficient.

Here is my dozen line named operator library:

namespace named_operator {
  template<class D>struct make_operator{make_operator(){}};

  template<class T, char, class O> struct half_apply { T&& lhs; };

  template<class Lhs, class Op>
  half_apply<Lhs, '*', Op> operator*( Lhs&& lhs, make_operator<Op> ) {
    return {std::forward<Lhs>(lhs)};
  }

  template<class Lhs, class Op, class Rhs>
  auto operator*( half_apply<Lhs, '*', Op>&& lhs, Rhs&& rhs )
  -> decltype( named_invoke( std::forward<Lhs>(lhs.lhs), Op{}, std::forward<Rhs>(rhs) ) )
  {
    return named_invoke( std::forward<Lhs>(lhs.lhs), Op{}, std::forward<Rhs>(rhs) );
  }
}

live example using it to implement vector *concat* vector. It only supports one operator, but extending it is easy. For serious use, I'd advise having a times function that by default calls invoke for *blah*, an add for +blah+ that does the same, etc. <blah> can directly call invoke.

Then the client programmer can overload an operator-specific overload and it works, or the general invoke.

Here is a similar library being used to implement *then* on both tuple-returning functions and futures.

Here is a primitive *in*:

namespace my_op {
  struct in_t:named_operator::make_operator<in_t>{};
  in_t in;

  template<class E, class C>
  bool named_invoke( E const& e, in_t, C const& container ) {
    using std::begin; using std::end;
    return std::find( begin(container), end(container), e ) != end(container);
  }
}
using my_op::in;

live example.

18
  • 33
    Whoa. Named operators. Mind. blown. Why have I never thought of this before? Feb 26, 2013 at 14:08
  • 2
    @KonradRudolph Here, have an implementation. make_infix<'*'>(arbitrary_binary_functor) returns a named operator on *. (everything below make_infix is various kinds of test code) Feb 26, 2013 at 14:26
  • 5
    @KonradRudolph the precedence of a named operator in C++ is the same as the "surrounding" operators. %op% thus ties with other multiplication/division operators in precedence, and <op> is looser than anything except assignment, logical, and == type operations. v1 *dot* v2 + v3 follows expected precedence rules, as does v0 *dot* v1 <cmp> v2 *dot* v3. Given that any binary operator works, and it has meaning, I simply let the user choose -- so make_infix<'%','<>'>(func) gives you your syntax. A nice side effect is vec1 +append= vec2; syntax reads real pretty as well. Feb 26, 2013 at 20:17
  • 2
    @noɥʇʎԀʎzɐɹƆ Get a better static code analyzer. Or just write template<class...Ts>void named_invoke(Ts&&...)=delete; in the named operator namespace before use, which could shut it up. Might not; is really working around a bug in the analyzer, so who knows what I need to do. Jan 20, 2017 at 1:51
  • 3
    @noɥʇʎԀʎzɐɹƆ Look 10^18 iterations in zero time. If you want help profiling something that compiles away to nothing, ask a SO question about it. There is nothing to profile, the named operator code doesn't exist in the output of an optimizing compiler. I'm sure there are compilers that fail to optimize it or cases where it cannot be done, but that is a quality of implementation issue in that particular compiler. Jan 20, 2017 at 18:27
12

See Boost Range Set algorithms. They still expect an output iterator though.

0
3

No and I think it never have something like that, this is a general principle in C++ that when you can have a non-member function to do the job never make that function a member. so it can't be like that, but may be Boost::Range help you.

1
  • 3
    I think OP’s beef wasn’t with the nonmember functions – the member function implementation was just an example. And there are trivial (and not so trivial) ways of making OP’s code way more concise with a proper library without having to use member function (Boost.Range does offer such a way, I believe). Feb 26, 2013 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.