1 // { dg-do run { target c++11 } }
2 // A basic implementation of TR1's bind using variadic teplates
3 // Contributed by Douglas Gregor <doug.gregor@gmail.com>
6 // Trivial reference_wrapper
8 struct reference_wrapper
10 reference_wrapper(T& x) : ptr(&x) { }
12 operator T&() const { return *ptr; }
14 T& get() const { return *ptr; }
19 template<typename T> reference_wrapper<T> ref(T& x) { return x; }
20 template<typename T> reference_wrapper<const T> cref(const T& x) { return x; }
22 // Simple type-traits we'll need
30 struct add_reference<T&>
35 template<typename T, typename U>
38 static const bool value = false;
44 static const bool value = true;
47 // For creating the constructor parameters of tuple<>
49 struct add_const_reference
51 typedef const T& type;
55 struct add_const_reference<T&>
60 // 6.1.3 Class template tuple: Needed for bind() implementation
61 template<typename... Values>
64 template<> class tuple<> { };
66 template<typename Head, typename... Tail>
67 class tuple<Head, Tail...>
68 : private tuple<Tail...>
70 typedef tuple<Tail...> inherited;
75 // implicit copy-constructor is okay
77 tuple(typename add_const_reference<Head>::type v,
78 typename add_const_reference<Tail>::type... vtail)
79 : m_head(v), inherited(vtail...) { }
81 template<typename... VValues>
82 tuple(const tuple<VValues...>& other)
83 : m_head(other.head()), inherited(other.tail()) { }
85 template<typename... VValues>
86 tuple& operator=(const tuple<VValues...>& other)
88 m_head = other.head();
89 tail() = other.tail();
93 typename add_reference<Head>::type head() { return m_head; }
94 typename add_reference<const Head>::type head() const { return m_head; }
95 inherited& tail() { return *this; }
96 const inherited& tail() const { return *this; }
103 struct make_tuple_result
109 struct make_tuple_result<reference_wrapper<T> >
114 // 6.1.3.2 Tuple creation functions
116 template<typename T> ignore_t& operator=(const T&) { return *this; }
119 template<typename... Values>
120 tuple<typename make_tuple_result<Values>::type...>
121 make_tuple(const Values&... values)
123 return tuple<typename make_tuple_result<Values>::type...>(values...);
126 template<typename... Values>
127 tuple<Values&...> tie(Values&... values)
129 return tuple<Values&...>(values...);
132 // 6.1.3.3 Tuple helper classes
133 template<typename Tuple>
137 struct tuple_size<tuple<> >
139 static const __SIZE_TYPE__ value = 0;
142 template<typename Head, typename... Tail>
143 struct tuple_size<tuple<Head, Tail...> >
145 static const __SIZE_TYPE__ value = 1 + tuple_size<tuple<Tail...> >::value;
148 template<int I, typename Tuple>
149 struct tuple_element;
151 template<int I, typename Head, typename... Tail>
152 struct tuple_element<I, tuple<Head, Tail...> >
154 typedef typename tuple_element<I-1, tuple<Tail...> >::type type;
157 template<typename Head, typename... Tail>
158 struct tuple_element<0, tuple<Head, Tail...> >
163 // 6.1.3.4 Element access
164 template<int I, typename Tuple>
167 template<int I, typename Head, typename... Values>
168 class get_impl<I, tuple<Head, Values...> >
170 typedef typename tuple_element<I-1, tuple<Values...> >::type Element;
171 typedef typename add_reference<Element>::type RJ;
172 typedef typename add_const_reference<Element>::type PJ;
173 typedef get_impl<I-1, tuple<Values...> > Next;
176 static RJ get(tuple<Head, Values...>& t)
177 { return Next::get(t.tail()); }
179 static PJ get(const tuple<Head, Values...>& t)
180 { return Next::get(t.tail()); }
183 template<typename Head, typename... Values>
184 class get_impl<0, tuple<Head, Values...> >
186 typedef typename add_reference<Head>::type RJ;
187 typedef typename add_const_reference<Head>::type PJ;
190 static RJ get(tuple<Head, Values...>& t) { return t.head(); }
191 static PJ get(const tuple<Head, Values...>& t) { return t.head(); }
194 template<int I, typename... Values>
195 typename add_reference<
196 typename tuple_element<I, tuple<Values...> >::type
198 get(tuple<Values...>& t)
200 return get_impl<I, tuple<Values...> >::get(t);
203 template<int I, typename... Values>
204 typename add_const_reference<
205 typename tuple_element<I, tuple<Values...> >::type
207 get(const tuple<Values...>& t)
209 return get_impl<I, tuple<Values...> >::get(t);
212 // 6.1.3.5 Relational operators
213 inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
215 template<typename T, typename... TTail, typename U, typename... UTail>
216 bool operator==(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
218 return t.head() == u.head() && t.tail() == u.tail();
221 template<typename... TValues, typename... UValues>
222 bool operator!=(const tuple<TValues...>& t, const tuple<UValues...>& u)
227 inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
229 template<typename T, typename... TTail, typename U, typename... UTail>
230 bool operator<(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
232 return (t.head() < u.head() ||
233 (!(t.head() < u.head()) && t.tail() < u.tail()));
236 template<typename... TValues, typename... UValues>
237 bool operator>(const tuple<TValues...>& t, const tuple<UValues...>& u)
242 template<typename... TValues, typename... UValues>
243 bool operator<=(const tuple<TValues...>& t, const tuple<UValues...>& u)
248 template<typename... TValues, typename... UValues>
249 bool operator>=(const tuple<TValues...>& t, const tuple<UValues...>& u)
254 // enable_if, the breakfast of champions
255 template<bool Cond, typename Type = void>
260 template<typename Type>
261 struct enable_if<false, Type> { };
263 // 3.6 Function object binders
265 // 3.6.1 Class template is_bind_expression
267 struct is_bind_expression {
268 static const bool value = false;
271 // 3.6.2 Class template is_placeholder
273 struct is_placeholder {
274 static const int value = 0;
277 // 3.6.3 Function template bind
278 template<int I> struct placeholder {} ;
280 template<int N> struct int_c { };
282 // A tuple of integer values
283 template<int...> struct int_tuple {};
285 // make_indexes_impl is a helper for make_indexes
286 template<int I, typename IntTuple, typename... Types>
287 struct make_indexes_impl;
290 template<int I, int... Indexes, typename T, typename... Types>
291 struct make_indexes_impl<I, int_tuple<Indexes...>, T, Types...>
293 typedef typename make_indexes_impl<I+1,
294 int_tuple<Indexes..., I>,
295 Types...>::type type;
298 template<int I, int... Indexes>
299 struct make_indexes_impl<I, int_tuple<Indexes...> > {
300 typedef int_tuple<Indexes...> type;
303 // make_indexes takes a variable-length number of N types and
304 // generates an int_tuple that contains <0, 1, 2, ..., N-1>. These can
305 // be used as indexes for tuple's get or tuple_element operation.
306 template<typename... Types>
307 struct make_indexes : make_indexes_impl<0, int_tuple<>, Types...> { };
309 // Get the Ith tuple element, but only if I is in bounds.
310 template<int I, typename Tuple, typename = void>
311 struct safe_tuple_element{ };
313 template<int I, typename... Values>
314 struct safe_tuple_element<I, tuple<Values...>,
315 typename enable_if<(I >= 0 &&
316 I < tuple_size<tuple<Values...> >::value)
319 typedef typename tuple_element<I, tuple<Values...> >::type type;
322 // mu maps a bound argument to an actual argument, given a tuple of
323 // the arguments passed to the function object returned by bind().
325 // Return the stored reference from reference_wrapper
326 template<typename T, typename... Args>
327 inline T& mu(reference_wrapper<T>& bound_arg, const tuple<Args&...>&)
329 return bound_arg.get();
332 // Unwrap a tuple into separate arguments and forward to the function
334 template<typename F, int... Indexes, typename... Args>
335 inline typename F::result_type
336 unwrap_and_forward(F& f, int_tuple<Indexes...>, const tuple<Args&...>& args)
338 return f(get<Indexes>(args)...);
341 // Evaluate the inner bind expression
342 template<typename Bound, typename... Args>
343 inline typename enable_if<is_bind_expression<Bound>::value,
344 typename Bound::result_type>::type
345 mu(Bound& bound_arg, const tuple<Args&...>& args)
347 typedef typename make_indexes<Args...>::type Indexes;
348 return unwrap_and_forward(bound_arg, Indexes(), args);
351 // Retrieve the Ith argument from args
352 template<typename Bound, typename... Args>
353 inline typename safe_tuple_element<is_placeholder<Bound>::value - 1,
354 tuple<Args...> >::type
355 mu(Bound& bound_arg, const tuple<Args&...>& args)
357 return get<is_placeholder<Bound>::value-1>(args);
360 // Return the stored value.
362 struct is_reference_wrapper {
363 static const bool value = false;
367 struct is_reference_wrapper<reference_wrapper<T> > {
368 static const bool value = true;
371 template<typename Bound, typename... Args>
372 inline typename enable_if<(!is_bind_expression<Bound>::value
373 && !is_placeholder<Bound>::value
374 && !is_reference_wrapper<Bound>::value),
376 mu(Bound& bound_arg, const tuple<Args&...>&)
382 template<typename F, typename... BoundArgs, int... Indexes, typename... Args>
383 typename F::result_type
384 apply_functor(F& f, tuple<BoundArgs...>& bound_args, int_tuple<Indexes...>,
385 const tuple<Args&...>& args)
387 return f(mu(get<Indexes>(bound_args), args)...);
390 template<typename F, typename... BoundArgs>
393 typedef typename make_indexes<BoundArgs...>::type indexes;
396 typedef typename F::result_type result_type;
398 explicit bound_functor(const F& f, const BoundArgs&... bound_args)
399 : f(f), bound_args(bound_args...) { }
401 template<typename... Args>
402 typename F::result_type operator()(Args&... args) {
403 return apply_functor(f, bound_args, indexes(), tie(args...));
408 tuple<BoundArgs...> bound_args;
411 template<typename F, typename... BoundArgs>
412 struct is_bind_expression<bound_functor<F, BoundArgs...> > {
413 static const bool value = true;
416 template<typename F, typename... BoundArgs>
417 inline bound_functor<F, BoundArgs...>
418 bind(const F& f, const BoundArgs&... bound_args)
420 return bound_functor<F, BoundArgs...>(f, bound_args...);
424 // 3.6.4 Placeholders
426 struct is_placeholder<placeholder<I> > {
427 static const int value = I;
443 typedef T result_type;
445 T operator()(T x, T y) { return x + y; }
450 typedef T result_type;
452 T operator()(T x, T y) { return x * y; }
457 typedef T result_type;
459 T operator()(T x) { return -x; }
467 assert(bind(plus<int>(), _1, _2)(seventeen, forty_two) == 59);
468 assert(bind(plus<int>(), _1, _1)(seventeen, forty_two) == 34);
469 assert(bind(plus<int>(), _2, _1)(seventeen, forty_two) == 59);
470 assert(bind(plus<int>(), 5, _1)(seventeen, forty_two) == 22);
471 assert(bind(plus<int>(), ref(seventeen), _2)(seventeen, forty_two) == 59);
472 assert(bind(plus<int>(), bind(multiplies<int>(), 3, _1), _2)(seventeen, forty_two)