language: C++11 (gcc-4.7.2)
date: 517 days 0 hours ago
link:
visibility: public
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <utility>
 
template <typename F, typename Arg>
struct Callable
{
private:
  typedef char                      yes;
  typedef struct { char array[2]; } no;
 
  template <typename G, typename Brg>
  static yes test(decltype(std::declval<G>()(std::declval<Brg>())) (G::*pfn)(Brg));
 
  template <typename G, typename Brg>
  static yes test(decltype(std::declval<G>()(std::declval<Brg>())) (G::*pfn)(Brg) const);
 
  template <typename G, typename Brg>
  static no test(...);
 
public:
  static bool const value = sizeof(test<F, Arg>(&F::operator())) == sizeof(yes);
};
 
struct Foo
{
  int operator()(int &) { return 1; }
 
};
 
struct Bar
{
  int operator()(int const &) { return 2; }
};
 
struct Wazz
{
  int operator()(int const &) const { return 3; }
};
 
struct Frob
{
  int operator()(int &) { return 4; }
  int operator()(int const &) const { return 5; }
};
 
struct Boom
{
 
};
 
#include <iostream>
int main()
{
  std::cout << "Foo(const int &):  " << Callable<Foo,  int const &>::value << std::endl
            << "Foo(int &):        " << Callable<Foo,  int &>::value << std::endl
            << "Bar(const int &):  " << Callable<Bar,  const int &>::value << std::endl
            << "Bar(int &):        " << Callable<Bar,  int &>::value << std::endl
            << "Wazz(const int &): " << Callable<Wazz, const int &>::value << std::endl
            << "Wazz(int &):       " << Callable<Wazz, int &>::value << std::endl
            << "Frob(const int &): " << Callable<Frob, const int &>::value << std::endl
            << "Frob(int &):       " << Callable<Frob, int &>::value << std::endl
            << "Boom(const int &): " << Callable<Boom, const int &>::value << std::endl
            << "Boom(int&):        " << Callable<Boom, int &>::value << std::endl
    ;
}
prog.cpp: In instantiation of 'const bool Callable<Boom, const int&>::value':
prog.cpp:61:70:   instantiated from here
prog.cpp:20:79: error: 'operator()' is not a member of 'Boom'
prog.cpp: In instantiation of 'const bool Callable<Boom, int&>::value':
prog.cpp:62:64:   instantiated from here
prog.cpp:20:79: error: 'operator()' is not a member of 'Boom'