Boost logo

Boost :

From: Bronek Kozicki (brok_at_[hidden])
Date: 2004-11-13 07:25:32


Jonathan Turkanis wrote:
> Hi All,
>
> I'm writing a macro-based interface definition language, which I hope to
> post soon. For this, I need to be able to detect the presence of member
> functions with given name and signature. My current formulation generates code
> like the following to test whether a class has a non-const member function named
> 'one' with the signature char(int, float):

Attached code works in MSVC71. Maybe this will give you some idea.

>cl /GX /GR T543.cpp

/out:T543.exe
T543.obj

> T543
1
0
0
0
0
1

B.


#include <iostream>

template <typename Type>
class has_member__one
{
  // 1. SIGNATURE goes here
  template <char (Type::*)(int, float)> struct helper{};

  typedef char (&yes) [1];
  typedef char (&no) [2];

  template <typename Type_>
  // 2. NAME goes here
  static yes test (helper<&Type_::one>*);

  template <typename Type_>
  static no test (...);

public:
    enum {value = sizeof(test<Type>(0)) == sizeof(yes)};
};

struct A // 1
{
  char one(int, float);
};

struct B // 0
{
  int one;
};

class C // 0
{
};

struct D // 0
{
  float one();
  void one(int, float);
  void one(int, int);
};

struct E // 0
{
  int one(int);
  int one(int, int);
  char one(int, float) const;
};

struct F // 1
{
  void one(int);
  char one(int, int);
  char one(int, float);
  char one(int, float) const;
};

int main()
{
  std::cout << has_member__one<A>::value << std::endl;
  std::cout << has_member__one<B>::value << std::endl;
  std::cout << has_member__one<C>::value << std::endl;
  std::cout << has_member__one<D>::value << std::endl;
  std::cout << has_member__one<E>::value << std::endl;
  std::cout << has_member__one<F>::value << std::endl;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk