Boost logo

Boost :

Subject: Re: [boost] Is there interest in a library for testing existence of a function member of class?
From: ºúά (huwei04_at_[hidden])
Date: 2014-11-23 08:03:20

> The most recent time I needed something like this, I wanted to test
> whether a struct contained a field with a particular name that was
> convertible to a float (I think float or int16_t in that case).
> I would have been happy with a test for a field with that name of
> any type. But I couldn't work out how to do it. Any ideas?

I think below code(incomplete) can test a field with a name of any type.
template <int c>struct enable{ typedef void type;};
template <typename T>struct wrapper{ static T value;};
template <typename T, typename E=void>struct has_foo_impl{ enum { value = false };};
template <typename T>struct has_foo_impl<const T, typename enable<sizeof(wrapper<T>, 1)>::type>{ enum { value = true };};
template <typename T>struct has_foo : has_foo_impl<const T> {};
struct a{};struct b{ int foo; };
#include <iostream>
int main (){ std::cout << has_foo<a>::value << std::endl; std::cout << has_foo<b>::value << std::endl; return 0;}

Boost list run by bdawes at, gregod at, cpdaniel at, john at