summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp0x/variadic-mem_fn.C
blob: 51c5c79ef9f4909079ec663cff7f8be716b876c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// { dg-options "-std=gnu++0x" }
// { dg-do run }
// A basic implementation of TR1's mem_fn using variadic teplates
// Contributed by Douglas Gregor <doug.gregor@gmail.com>
#include <cassert>

template<typename R, typename Class, typename... Args>
class Mem_fn
{
 public:
  explicit Mem_fn(R (Class::*pmf)(Args...)) : pmf(pmf) { }

  R operator()(Class& object, Args... args)
  {
    return (object.*pmf)(args...);
  }

  R operator()(Class* object, Args... args)
  {
    return (object->*pmf)(args...);
  }

  R (Class::*pmf)(Args...);
};

template<typename R, typename Class, typename... Args>  
inline Mem_fn<R, Class, Args...>
mem_fn(R (Class::* pmf)(Args...))
{
  return Mem_fn<R, Class, Args...>(pmf);
}

class X {
 public:
  int negate(int x) { return -x; }
  int plus(int x, int y) { return x + y; }
};

int main()
{
  X x;
  X* xp = &x;

  assert(mem_fn(&X::negate)(x, 17) == -17);
  assert(mem_fn(&X::negate)(xp, 17) == -17);
  assert(mem_fn(&X::plus)(x, 17, 25) == 42);
  assert(mem_fn(&X::plus)(xp, 17, 25) == 42);

  return 0;
}