language: C++11 (gcc-4.7.2)
date: 492 days 16 hours ago
link:
visibility: private
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <utility>
#include <type_traits>
 
template <typename F, typename... Args>
class Callable{
  static int tester[1];
  typedef char yes;
  typedef yes (&no)[2];
 
  template <typename G, typename... Brgs, typename C>
  static typename std::enable_if<!std::is_same<G,C>::value, char>::type
      sfinae(decltype(std::declval<G>()(std::declval<Brgs>()...)) (C::*pfn)(Brgs...));
 
  template <typename G, typename... Brgs, typename C>
  static typename std::enable_if<!std::is_same<G,C>::value, char>::type
      sfinae(decltype(std::declval<G>()(std::declval<Brgs>()...)) (C::*pfn)(Brgs...) const);
 
  template <typename G, typename... Brgs>
  static char sfinae(decltype(std::declval<G>()(std::declval<Brgs>()...)) (G::*pfn)(Brgs...));
 
  template <typename G, typename... Brgs>
  static char sfinae(decltype(std::declval<G>()(std::declval<Brgs>()...)) (G::*pfn)(Brgs...) const);
 
  template <typename G, typename... Brgs>
  static yes test(int (&a)[sizeof(sfinae<G,Brgs...>(&G::operator()))]);
 
  template <typename G, typename... Brgs>
  static no test(...);
 
public:
  static bool const value = sizeof(test<F, Args...>(tester)) == sizeof(yes);
};
 
template<class R, class... Args>
struct Helper{ R operator()(Args...); };
 
template<typename R, typename... FArgs, typename... Args>
class Callable<R(*)(FArgs...), Args...>
  : public Callable<Helper<R, FArgs...>, Args...>{};
 
struct Foo{
  int operator()(int &) { return 1; }
};
 
struct Bar{
  int operator()(int const &) { return 2; }
};
 
struct Frob{
  int operator()(int &) { return 4; }
  int operator()(int const &) const { return 5; }
};
 
struct Blip{
  template<typename T>
  int operator()(T) { return 6; }
};
 
struct Boom{
  int operator()(const int &) { return 2; }
  int operator()(float x) { return 0;}
};
 
struct Zap{
  int operator()(int) { return 42; }
};
 
typedef int (*Zip)(int const&);
typedef int (*Zup)(float);
 
struct Ping{
  int operator()(int const&, char=0){ return 42; }
};
 
struct Inh : Bar{};
 
 
#include <iostream>
 
template<class T>
void test(int ref, int cref){
  static bool const ref_val = Callable<T, int&>::value;
  static bool const cref_val = Callable<T, int const&>::value;
  std::cout << "\t(int&) test - expected: " << ref << " - got: "
              << ref_val << '\n'
            << "\t(int const&) test - expected: " << cref << " - got: "
              << cref_val << '\n';
  if(ref == ref_val && cref == cref_val)
    std::cout << "passed\n";
  else
    std::cout << "FAILED\n";
}
 
int main()
{
  std::cout << "Foo - no overload - 'int&' parameter:\n";
            test<Foo>(1, 0);
  std::cout << "Bar - no overload - 'int const&' parameter:\n";
            test<Bar>(0, 1);
  std::cout << "Zap - no overload - 'int' parameter:\n";
            test<Zap>(0, 0);
  std::cout << "Frob - overloaded - 'int&' and 'int const&' parameter:\n";
            test<Frob>(1, 1);
  std::cout << "Boom - overloaded - 'int const&' and 'float' parameter:\n";
            test<Boom>(0, 1);
  std::cout << "Blip - templated - 'T' parameter:\n";
            test<Blip>(1, 1);
  std::cout << "Zip - function pointer - 'int const&' parameter:\n";
            test<Zip>(0, 1);
  std::cout << "Zup - function pointer - 'float' parameter:\n";
            test<Zup>(0, 0);
  std::cout << "Ping - no overload - 'int const&' and 'char=0' parameter:\n";
            test<Ping>(0, 1);
  std::cout << "Inh - no overload, inherited - 'int const&' parameter:\n";
            test<Inh>(0, 1);
}
  • upload with new input
  • result: Success     time: 0s    memory: 2832 kB     returned value: 0

    Foo - no overload - 'int&' parameter:
    	(int&) test - expected: 1 - got: 1
    	(int const&) test - expected: 0 - got: 0
    passed
    Bar - no overload - 'int const&' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 1 - got: 1
    passed
    Zap - no overload - 'int' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 0 - got: 0
    passed
    Frob - overloaded - 'int&' and 'int const&' parameter:
    	(int&) test - expected: 1 - got: 0
    	(int const&) test - expected: 1 - got: 0
    FAILED
    Boom - overloaded - 'int const&' and 'float' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 1 - got: 0
    FAILED
    Blip - templated - 'T' parameter:
    	(int&) test - expected: 1 - got: 1
    	(int const&) test - expected: 1 - got: 1
    passed
    Zip - function pointer - 'int const&' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 1 - got: 1
    passed
    Zup - function pointer - 'float' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 0 - got: 0
    passed
    Ping - no overload - 'int const&' and 'char=0' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 1 - got: 1
    passed
    Inh - no overload, inherited - 'int const&' parameter:
    	(int&) test - expected: 0 - got: 0
    	(int const&) test - expected: 1 - got: 1
    passed