1 // { dg-options "-std=gnu++0x" }
3 // A basic implementation of TR1's bind using variadic teplates
4 // Contributed by Douglas Gregor <doug.gregor@gmail.com>
7 // Trivial reference_wrapper
9 struct reference_wrapper
11 reference_wrapper(T& x) : ptr(&x) { }
13 operator T&() const { return *ptr; }
15 T& get() const { return *ptr; }
20 template<typename T> reference_wrapper<T> ref(T& x) { return x; }
21 template<typename T> reference_wrapper<const T> cref(const T& x) { return x; }
23 // Simple type-traits we'll need
31 struct add_reference<T&>
36 template<typename T, typename U>
39 static const bool value = false;
45 static const bool value = true;
48 // For creating the constructor parameters of tuple<>
50 struct add_const_reference
52 typedef const T& type;
56 struct add_const_reference<T&>
61 // 6.1.3 Class template tuple: Needed for bind() implementation
62 template<typename... Values>
65 template<> class tuple<> { };
67 template<typename Head, typename... Tail>
68 class tuple<Head, Tail...>
69 : private tuple<Tail...>
71 typedef tuple<Tail...> inherited;
76 // implicit copy-constructor is okay
78 tuple(typename add_const_reference<Head>::type v,
79 typename add_const_reference<Tail>::type... vtail)
80 : m_head(v), inherited(vtail...) { }
82 template<typename... VValues>
83 tuple(const tuple<VValues...>& other)
84 : m_head(other.head()), inherited(other.tail()) { }
86 template<typename... VValues>
87 tuple& operator=(const tuple<VValues...>& other)
89 m_head = other.head();
90 tail() = other.tail();
94 typename add_reference<Head>::type head() { return m_head; }
95 typename add_reference<const Head>::type head() const { return m_head; }
96 inherited& tail() { return *this; }
97 const inherited& tail() const { return *this; }
104 struct make_tuple_result
110 struct make_tuple_result<reference_wrapper<T> >
115 // 6.1.3.2 Tuple creation functions
117 template<typename T> ignore_t& operator=(const T&) { return *this; }
120 template<typename... Values>
121 tuple<typename make_tuple_result<Values>::type...>
122 make_tuple(const Values&... values)
124 return tuple<typename make_tuple_result<Values>::type...>(values...);
127 template<typename... Values>
128 tuple<Values&...> tie(Values&... values)
130 return tuple<Values&...>(values...);
133 // 6.1.3.3 Tuple helper classes
134 template<typename Tuple>
138 struct tuple_size<tuple<> >
140 static const __SIZE_TYPE__ value = 0;
143 template<typename Head, typename... Tail>
144 struct tuple_size<tuple<Head, Tail...> >
146 static const __SIZE_TYPE__ value = 1 + tuple_size<tuple<Tail...> >::value;
149 template<int I, typename Tuple>
150 struct tuple_element;
152 template<int I, typename Head, typename... Tail>
153 struct tuple_element<I, tuple<Head, Tail...> >
155 typedef typename tuple_element<I-1, tuple<Tail...> >::type type;
158 template<typename Head, typename... Tail>
159 struct tuple_element<0, tuple<Head, Tail...> >
164 // 6.1.3.4 Element access
165 template<int I, typename Tuple>
168 template<int I, typename Head, typename... Values>
169 class get_impl<I, tuple<Head, Values...> >
171 typedef typename tuple_element<I-1, tuple<Values...> >::type Element;
172 typedef typename add_reference<Element>::type RJ;
173 typedef typename add_const_reference<Element>::type PJ;
174 typedef get_impl<I-1, tuple<Values...> > Next;
177 static RJ get(tuple<Head, Values...>& t)
178 { return Next::get(t.tail()); }
180 static PJ get(const tuple<Head, Values...>& t)
181 { return Next::get(t.tail()); }
184 template<typename Head, typename... Values>
185 class get_impl<0, tuple<Head, Values...> >
187 typedef typename add_reference<Head>::type RJ;
188 typedef typename add_const_reference<Head>::type PJ;
191 static RJ get(tuple<Head, Values...>& t) { return t.head(); }
192 static PJ get(const tuple<Head, Values...>& t) { return t.head(); }
195 template<int I, typename... Values>
196 typename add_reference<
197 typename tuple_element<I, tuple<Values...> >::type
199 get(tuple<Values...>& t)
201 return get_impl<I, tuple<Values...> >::get(t);
204 template<int I, typename... Values>
205 typename add_const_reference<
206 typename tuple_element<I, tuple<Values...> >::type
208 get(const tuple<Values...>& t)
210 return get_impl<I, tuple<Values...> >::get(t);
213 // 6.1.3.5 Relational operators
214 inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
216 template<typename T, typename... TTail, typename U, typename... UTail>
217 bool operator==(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
219 return t.head() == u.head() && t.tail() == u.tail();
222 template<typename... TValues, typename... UValues>
223 bool operator!=(const tuple<TValues...>& t, const tuple<UValues...>& u)
228 inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
230 template<typename T, typename... TTail, typename U, typename... UTail>
231 bool operator<(const tuple<T, TTail...>& t, const tuple<U, UTail...>& u)
233 return (t.head() < u.head() ||
234 (!(t.head() < u.head()) && t.tail() < u.tail()));
237 template<typename... TValues, typename... UValues>
238 bool operator>(const tuple<TValues...>& t, const tuple<UValues...>& u)
243 template<typename... TValues, typename... UValues>
244 bool operator<=(const tuple<TValues...>& t, const tuple<UValues...>& u)
249 template<typename... TValues, typename... UValues>
250 bool operator>=(const tuple<TValues...>& t, const tuple<UValues...>& u)
255 // enable_if, the breakfast of champions
256 template<bool Cond, typename Type = void>
261 template<typename Type>
262 struct enable_if<false, Type> { };
264 // 3.6 Function object binders
266 // 3.6.1 Class template is_bind_expression
268 struct is_bind_expression {
269 static const bool value = false;
272 // 3.6.2 Class template is_placeholder
274 struct is_placeholder {
275 static const int value = 0;
278 // 3.6.3 Function template bind
279 template<int I> struct placeholder {} ;
281 template<int N> struct int_c { };
283 // A tuple of integer values
284 template<int...> struct int_tuple {};
286 // make_indexes_impl is a helper for make_indexes
287 template<int I, typename IntTuple, typename... Types>
288 struct make_indexes_impl;
291 template<int I, int... Indexes, typename T, typename... Types>
292 struct make_indexes_impl<I, int_tuple<Indexes...>, T, Types...>
294 typedef typename make_indexes_impl<I+1,
295 int_tuple<Indexes..., I>,
296 Types...>::type type;
299 template<int I, int... Indexes>
300 struct make_indexes_impl<I, int_tuple<Indexes...> > {
301 typedef int_tuple<Indexes...> type;
304 // make_indexes takes a variable-length number of N types and
305 // generates an int_tuple that contains <0, 1, 2, ..., N-1>. These can
306 // be used as indexes for tuple's get or tuple_element operation.
307 template<typename... Types>
308 struct make_indexes : make_indexes_impl<0, int_tuple<>, Types...> { };
310 // Get the Ith tuple element, but only if I is in bounds.
311 template<int I, typename Tuple, typename = void>
312 struct safe_tuple_element{ };
314 template<int I, typename... Values>
315 struct safe_tuple_element<I, tuple<Values...>,
316 typename enable_if<(I >= 0 &&
317 I < tuple_size<tuple<Values...> >::value)
320 typedef typename tuple_element<I, tuple<Values...> >::type type;
323 // mu maps a bound argument to an actual argument, given a tuple of
324 // the arguments passed to the function object returned by bind().
326 // Return the stored reference from reference_wrapper
327 template<typename T, typename... Args>
328 inline T& mu(reference_wrapper<T>& bound_arg, const tuple<Args&...>&)
330 return bound_arg.get();
333 // Unwrap a tuple into separate arguments and forward to the function
335 template<typename F, int... Indexes, typename... Args>
336 inline typename F::result_type
337 unwrap_and_forward(F& f, int_tuple<Indexes...>, const tuple<Args&...>& args)
339 return f(get<Indexes>(args)...);
342 // Evaluate the inner bind expression
343 template<typename Bound, typename... Args>
344 inline typename enable_if<is_bind_expression<Bound>::value,
345 typename Bound::result_type>::type
346 mu(Bound& bound_arg, const tuple<Args&...>& args)
348 typedef typename make_indexes<Args...>::type Indexes;
349 return unwrap_and_forward(bound_arg, Indexes(), args);
352 // Retrieve the Ith argument from args
353 template<typename Bound, typename... Args>
354 inline typename safe_tuple_element<is_placeholder<Bound>::value - 1,
355 tuple<Args...> >::type
356 mu(Bound& bound_arg, const tuple<Args&...>& args)
358 return get<is_placeholder<Bound>::value-1>(args);
361 // Return the stored value.
363 struct is_reference_wrapper {
364 static const bool value = false;
368 struct is_reference_wrapper<reference_wrapper<T> > {
369 static const bool value = true;
372 template<typename Bound, typename... Args>
373 inline typename enable_if<(!is_bind_expression<Bound>::value
374 && !is_placeholder<Bound>::value
375 && !is_reference_wrapper<Bound>::value),
377 mu(Bound& bound_arg, const tuple<Args&...>&)
383 template<typename F, typename... BoundArgs, int... Indexes, typename... Args>
384 typename F::result_type
385 apply_functor(F& f, tuple<BoundArgs...>& bound_args, int_tuple<Indexes...>,
386 const tuple<Args&...>& args)
388 return f(mu(get<Indexes>(bound_args), args)...);
391 template<typename F, typename... BoundArgs>
394 typedef typename make_indexes<BoundArgs...>::type indexes;
397 typedef typename F::result_type result_type;
399 explicit bound_functor(const F& f, const BoundArgs&... bound_args)
400 : f(f), bound_args(bound_args...) { }
402 template<typename... Args>
403 typename F::result_type operator()(Args&... args) {
404 return apply_functor(f, bound_args, indexes(), tie(args...));
409 tuple<BoundArgs...> bound_args;
412 template<typename F, typename... BoundArgs>
413 struct is_bind_expression<bound_functor<F, BoundArgs...> > {
414 static const bool value = true;
417 template<typename F, typename... BoundArgs>
418 inline bound_functor<F, BoundArgs...>
419 bind(const F& f, const BoundArgs&... bound_args)
421 return bound_functor<F, BoundArgs...>(f, bound_args...);
425 // 3.6.4 Placeholders
427 struct is_placeholder<placeholder<I> > {
428 static const int value = I;
444 typedef T result_type;
446 T operator()(T x, T y) { return x + y; }
451 typedef T result_type;
453 T operator()(T x, T y) { return x * y; }
458 typedef T result_type;
460 T operator()(T x) { return -x; }
468 assert(bind(plus<int>(), _1, _2)(seventeen, forty_two) == 59);
469 assert(bind(plus<int>(), _1, _1)(seventeen, forty_two) == 34);
470 assert(bind(plus<int>(), _2, _1)(seventeen, forty_two) == 59);
471 assert(bind(plus<int>(), 5, _1)(seventeen, forty_two) == 22);
472 assert(bind(plus<int>(), ref(seventeen), _2)(seventeen, forty_two) == 59);
473 assert(bind(plus<int>(), bind(multiplies<int>(), 3, _1), _2)(seventeen, forty_two)