12:01 AM
Hi Luc
 
Oh hello.
 
First time using chat... didn't even know it existed :)
 
Some 'communities' are quite active with it, including C++.
 
Cool!
I was thinking some more about the polymorphic functor issue
The use case I was thinking of involved applying a function to e.g. a Boost.Fusion sequence
I mean, to elements of the sequence
In this case, and similar cases, one should be able to obtain an MPL sequence of the types the function will be called on
 
I wrote my make_overload helper for visiting variants, the use cases are somewhat similar then.
 
12:05 AM
If we can get that AND the size of the sequence as a preprocessor token (I think this may be the difficult part)
 
Well that last bit is simply not doable: just imagine two instantiations with varying sequence lengths.
 
Then the preprocessor can loop from 1 to N
 
The preprocessor token can't have two different values.
 
and generate tokens of the form mpl::at<sequence, 1>::type etc.
right
 
Also if we're assuming C++0x note that the repetition is not needed to access the types of the elements, variadic templates can't handle that.
 
12:06 AM
yeah that's a problem...
ok
 
The two key parts is repeating the name as many times as needed, and to coerce the name for each type of the sequence.
 
so we can do it with an MPL sequence variadic templates?
I mean, MPL sequence and variadic templates
 
Well these days I don't really use Boost.MPL sequences, I simply pass types as variadic template parameters and/or pack them into std::tuple in lieu of e.g. mpl::vector.
 
sure
but do variadic templates help with the preprocessing issue?
 
No.
 
12:09 AM
ok
so how does C++0x help, then?
 
Don't need the mpl::at<Sequence, n> stuff.
 
Hmm
I think I'm still missing something
 
Right.
 
We want the macro to expand to something like
[](int arg0) { return f(arg0); }
, [](double arg0) { return f(arg0); }
The preprocessor can't get "int" and "double" tokens out of the MPL sequence
 
I'm thinking of a differect solution; allow me.
 
12:12 AM
but it can get mpl::at<sequence, 0>::type and mpl::at<sequence, 1>::type tokens
so, if it knows N
it can generate
[](mpl::at<sequence, 0>::type arg0) { return f(arg0); }
, [](mpl::at<sequence, 1>::type arg0) { return f(arg0); }
sorry, go ahead
 
template<typename... T> struct with_sequence { static unspecified make_overload(T const&... t); };
Then if we instantiate with_sequence<void(int), void(double)>
The signature is unspecified make_overload(void (&t0)(int), void(&t1)(double));
and make_overload(f, f) compiles.
i.e. the overloaded name f is coerced to the types of the sequence.
 
ah
and what would make_overload() return?
 
And then with_sequence<T...>::make_overload delegates to the 'real' make_overload.
so return type would be decltype( ::make_overload(t...) ) (assuming a global make_overload which should be enclosing_namespace::make_overload in real code)
 
i see
the only snag is we have to write "f" N times
 
FWIW my make_overload helper works with function references/pointers as well as objects.
@HighCommander4 Indeed
 
12:16 AM
as in with_sequence<seq>::make_overload(f, f, f, ..., f)
 
I'm not overly familiar with Boost.PP but how would you feel with
 
we can make the preprocessor do the repetition
but, again, we need the size of the sequence as a PP token
 
POLYMORPHIC_LAMBDA( (sig0)(sig1)(sig2), f )?
Which would mean POLYMORPHIC_LAMBDA( (void(int))(void(double)), f )
 
the problem is, I don't know the types at preprocessing time
I can only get them as an MPL sequence or similar
could we give make_overload some dummy extra parameters
 
Oh right.
Generic solution.
 
12:18 AM
so you can do e.g. with_sequence<int, double>::make_overload(f, f, f, f, f, f, f, f, f, f)
and it would ignore the f's beyond the first 2?
 
That's smart.
 
then we could get the PP to repeat f say 10 times always
 
Yes, the signature would be as follows:
 
(which limits us to sequences of size 10)
 
template<typename... Ignored> auto make_overload(T const&... t, Ignored&&...) -> decltype /* rest as before */
I'm checking with my compiler.
 
12:20 AM
nice
 
And since make_overload would accept any number of arguments, it's just a matter of making the repetition a global macro.
e.g. POLYMORPHIC_LAMDA_MAX_ARITY
 
exactly!
so, ultimately, I would be able to write
for_each(fusion_seq, MAKE_OVERLOAD(mpl_seq, f))
 
Right.
 
the PP would expand MAKE_OVERLOAD(mpl_seq, f) to with_sequence<mpl_seq>::make_overload(f, f, f, f, f, f, f, f, f, f, f)
(there would have to be some trickery to convert the mpl seq types to a variadic param list, but that should be doable)
 
I can confirm that the double pack expansion of make_overload is accepted by GCC.
 
12:24 AM
nice!
 
I have written a fusion sequence <-> variadic template conversion somewhere, let me look it up.
Ah, it uses my indices facility.
(for the Fusion sequence -> variadic direction)
 
indices?
 
I use variadic templates a lot and there's a common problem that I hit.
Assume a functor f and a tuple. You want to 'unpack' the tuple and call f with its elements.
It's surprisingly not easy to do.
 
ah, yes
i remember doing that at some point
(or, rather, searching SO for how to do that and finding somethind :D)
 
given a variadic sequence like std::tuple<T...>, the problem is ending with std::get<???>(tuple)...
(here we have Sequence, and need typename result_of::at_c<???, Sequence>::type)
Now it is possible to implement apply_tuple(functor, tuple) the 'hard' way.
 
12:30 AM
yes
i just found it, i did that
 
Right
You can do the same trick then.
 
yep
ok, last question
this is probably easy, but i can't think of it at the moment
how does the global make_overload work?
 
The thing is, after the third time of writing template<int... Indices> struct indices { typedef indices<I..., sizeof...(Indices)> type; }; I refactored it all.
And I use just one such indices type to do it all.
The unfortunate side is that it costs a function call to use it.
 
hmm
not sure where the indices come into play
this is how my apply_tuple works:
apply_tuple(f, t) calls tuple_applier<N>::apply(f, t)
where tuple_applier is a helper class
tuple_applier<N>::apply(f, t, varargs...) calls tuple_applier<N - 1>::apply(f, t, get<N - 1>(t), varargs...)
and tuple_applier<0>::apply(f, t, varargs...) calls f(varargs...)
 
Ah, a recursive solution.
 
12:36 AM
brb
 
IIRC I used one but I got it wrong. Some cases failed and due to SFINAE I couldn't track it down. Instead I used a solution that is used in libstdc++
 
I left out some details
e.g. figuring out the return type
 
(The difficult part was preserving value-category while still doing at most one move/copy)
Let's disregard that then.
 
hmm
for that
i think you should be ok as long tuple_applier::apply() takes varargs... by rvalue reference
and passes them on using 'forward'
but i could be missing something
 
Well the thing is
std::get<N>(rvalue) sometimes does a move forward.
 
12:41 AM
oh
 
e.g. std::get<0>( std::make_tuple(std::unique_ptr<int>()) ) returns std::unique_ptr<int>&&, not std::unique_ptr<int>&
So I wanted my apply_tuple to respect that (and now it does).
 
wait what?
why would you expect it to return std::unique_ptr<int&>?
 
Gah
Typo
Relevant point is that std::get returns cv T& for lvalues and cv T&& for rvalues.
So assuming typedef std::unique_ptr<int> pointer;
void f(pointer);
then apply_tuple(f, std::make_tuple(pointer())); compiles
while auto tuple = std::make_tuple(pointer()); apply_tuple(f, tuple); doesn't.
 
i see
i'll have to try that with mine
 
Moving on.
 
12:45 AM
yes
 
make_forward doesn't do any real work.
 
so, about the global make_overload
i can see how to write it for a fixed number of overloads
 
As the name implies, it returns an overload<T...>
 
do you need a separate class for every number of overloads?
 
No I only need a variadic template :)
 
12:46 AM
or can you do it with variadics somehow?
 
template<typename... T> class overload;
 
but how can you declare a variable number of operator() overloads in your class?
 
Well I have to reimplement std::tuple essentially.
 
some trick where you recursively derive from yourself?
 
Except with careful using of using Base::operator(); declarations
Now you're in luck.
Because it's the third time this week that I've been mentioning make_overload.
 
12:47 AM
i.e. overload<T, U, V, W> derives from overload<T, U, V> and adds an operator()(W)
 
So by the second time I actually extracted the code.
 
and overload<T, U, V> derives from overload<T, U> and adds an operator()(V)
and overload<T, U> is the base case?
is that what you had in mind?
 
Yes, that's how tuple is usually implemented (minus operator()).
 
i see
clever
 
template<typename Head, typename... Tail> class tuple_impl<Head, Tail...>: private Head, private tuple_impl<Tail...>
i.e. popping from the front, not the back.
Here's where you can find my code.
 
12:49 AM
right
sweet!
thanks
i will implement the macro/sequence stuff on top of this when i get a chance
(might be a few days)
and i'll be sure to share it with you
 
I may as well. But I'm seriously deathly afraid of Boost.Preprocessor :)
 
LOL
yeah, it's not my favourite tool either
but it can do some things that you just can't do otherwise
well thanks a lot for your help!
 
I'm regularly in Lounge<C++> in case you need to ask me something.
 
we've basically gotten as close as we can to emulating polymorphic lambdas
not bad!
cool
 
I'm quite impressed, yes.
 
12:53 AM
i think i'll start hanging out here too
in Lounge<C++>, i mean
 
I have to warn you: far from every discussion is C++ related!
 
:)
i can live with that
 
 
4 hours later…
4:43 AM
@HighCommander4 I'm getting around to implement MAKE_OVERLOAD and there's a hitch: I don't think it's possible to discard the superfluous names.
Given template<typename T> make(T&&);, then make(f) is an error if f is an overloaded name: it's not possible to pick one.
It's solvable though: I'll just arbitrary pick the first type of the sequence as a parameter.
So now it's template<typename First, typename... Rest> struct with_sequence<First, Rest...> { static unspecified make_overload(First const&, Rest... const&, First const&, First const&, First const&, First const& and so on (Boost.PP for that last repetition)
 
5:07 AM
Well, I think I have written an implementation. Unfortunately I can't test it because GCC doesn't behave well. It apparently is using deduction on the parameter pack Rest const&... whereas make_overload is a function, not a template!