constexpr и детектор методов
От: Шахтер Интернет  
Дата: 05.08.11 14:31
Оценка: 15 (3)
Облагороженный детектор методов с использование constexpr. Гонялось на gcc 4.6.1.

struct ProbeSet_method
 {
  template <class S,void (S::* M)()> struct Host;

  template <class S>
  static constexpr bool Probe(...) { return false; }

  template <class S>
  static constexpr bool Probe(Host<S,&S::method> *) { return true; }
 };

template <class T,class ProbeSet> 
struct Has
 {
  enum RetType { Ret = ProbeSet::template Probe<T>(0) };
 };

template <class T> 
struct Has_method : Has<T,ProbeSet_method> {};

struct Test1
 {
  void method();
 };

struct Test2
 {
  void method(int);
 };

#include <stdio.h>

int main()
 {
  printf("%d\n",Has_method<int>::Ret);
  printf("%d\n",Has_method<Test1>::Ret);
  printf("%d\n",Has_method<Test2>::Ret);

  return 0;
 }
В XXI век с CCore.
Копай Нео, копай -- летать научишься. © Matrix. Парадоксы
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.