File: sfinae64.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (69 lines) | stat: -rw-r--r-- 2,273 bytes parent folder | download | duplicates (2)
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
// PR c++/87748
// { dg-do compile { target c++11 } }

template <class T> T&& declval();
template <bool B, class T> struct enable_if;
template <class T> struct enable_if <true, T> { typedef T type; };
struct true_type { static const bool value = true; };
struct false_type { static const bool value = false; };

struct string
{
  string (const char *p);
};

/// Template to map all type arguments to void, useful for SFINAE, see also WG21 N3911.
template<class...> using void_t = void;

/// REQUIRES<value> - Simplified version of enable_if<> to use SFINAE in function templates.
template<bool value> using REQUIRES = typename ::enable_if<value, bool>::type;

/// DerivesString<T> - Check if @a T is of type 'string'.
template<class T> struct DerivesString
{
  static const int value = __is_same_as (T, string);
};

/// Has__aida_visit__<T,Visitor> - Check if @a T provides a member template __aida_visit__<>(Visitor).
template<class, class, class = void> struct Has__aida_visit__ : false_type {};
template<class T, class V>
struct Has__aida_visit__<T, V, void_t< decltype (declval<T>().template __aida_visit__<V> (*(V*) 0)) >> : true_type {};

struct Foo {
  template<class V> void __accept__ (V*);
};

/// Base template for Visitor classes, dispatches operator() to visit_<type>() methods.
template<class DerivedVisitor>
class VisitorDispatcher {
  DerivedVisitor* derived () { return static_cast<DerivedVisitor*> (this); }
protected:
  typedef const char* Name; ///< Member name argument type for visit() methods.
public:

  // dispatch for calls like: visitor (field, "field");

  template<class A,
           REQUIRES< (!Has__aida_visit__<A, DerivedVisitor>::value) > = true>
  void operator() (A &a, Name n)
  { return derived()->visit_string (a, n); }
};

class PspecVisitor : public VisitorDispatcher<PspecVisitor> {
public:
  void visit_string (::string &a, Name name);
};

int
main (int argc, char *argv[])
{
#ifdef WITHASSERT // define this to fix g++-8.1.1
  static_assert (Has__aida_visit__<string, PspecVisitor>::value == false, "");
#endif
  PspecVisitor v;
  string str ("A string");
  v (str, "some_string");
  static_assert (Has__aida_visit__<string, PspecVisitor>::value == false, "");
  return 0;
}
// g++ -std=gnu++14 -Wall -O2 aidavisit.cc && ./a.out