diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/g++.dg/cpp0x/variadic-function.C | |
download | cbb-gcc-4.6.4-upstream.tar.bz2 cbb-gcc-4.6.4-upstream.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/g++.dg/cpp0x/variadic-function.C')
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/variadic-function.C | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic-function.C b/gcc/testsuite/g++.dg/cpp0x/variadic-function.C new file mode 100644 index 000000000..eeb137705 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/variadic-function.C @@ -0,0 +1,96 @@ +// { dg-do run } +// { dg-options "-std=gnu++0x" } +// A basic implementation of TR1's function using variadic teplates +// Contributed by Douglas Gregor <doug.gregor@gmail.com> +#include <cassert> + +template<typename Signature> +class function; + +template<typename R, typename... Args> +class invoker_base +{ + public: + virtual ~invoker_base() { } + virtual R invoke(Args...) = 0; + virtual invoker_base* clone() = 0; +}; + +template<typename F, typename R, typename... Args> +class functor_invoker : public invoker_base<R, Args...> +{ + public: + explicit functor_invoker(const F& f) : f(f) { } + R invoke(Args... args) { return f(args...); } + functor_invoker* clone() { return new functor_invoker(f); } + + private: + F f; +}; + +template<typename R, typename... Args> +class function<R (Args...)> { + public: + typedef R result_type; + + function() : invoker (0) { } + + function(const function& other) : invoker(0) { + if (other.invoker) + invoker = other.invoker->clone(); + } + + template<typename F> + function(const F& f) : invoker(0) { + invoker = new functor_invoker<F, R, Args...>(f); + } + + ~function() { + if (invoker) + delete invoker; + } + + function& operator=(const function& other) { + function(other).swap(*this); + return *this; + } + + template<typename F> + function& operator=(const F& f) { + function(f).swap(*this); + return *this; + } + + void swap(function& other) { + invoker_base<R, Args...>* tmp = invoker; + invoker = other.invoker; + other.invoker = tmp; + } + + result_type operator()(Args... args) const { + assert(invoker); + return invoker->invoke(args...); + } + + private: + invoker_base<R, Args...>* invoker; +}; + +struct plus { + template<typename T> T operator()(T x, T y) { return x + y; } +}; + +struct multiplies { + template<typename T> T operator()(T x, T y) { return x * y; } +}; + +int main() +{ + function<int(int, int)> f1 = plus(); + assert(f1(3, 5) == 8); + + f1 = multiplies(); + assert(f1(3, 5) == 15); + + return 0; +} |