|
Boost : |
From: Vladimir Prus (ghost_at_[hidden])
Date: 2002-03-22 03:53:54
Aleksey Gurtovoy wrote:
> Seems that something like the following should give us an ability to check
> for presence of a particulary named member [function/data] in a class:
>
> typedef char (&no_tag)[1];
> typedef char (&yes_tag)[2];
>
> template< typename T > no_tag has_member_foo_helper(...);
>
> template< typename T >
> yes_tag has_member_foo_helper(int, void (T::*)() = &T::foo);
>
> template< typename T >
> struct has_member_foo
> {
> BOOST_STATIC_CONSTANT(bool
> , value = sizeof(has_member_foo_helper<T>(0)) == sizeof(yes_tag)
> );
> };
>
> struct my {};
> struct her { void foo(); };
>
> int main()
> {
> BOOST_STATIC_ASSERT(!has_member_foo<my>::value);
> BOOST_STATIC_ASSERT(has_member_foo<her>::value);
>
> return 0;
> }
With both g++ 3.0 and online Comeau compiler it just says that "my" has no
member "foo", which is supposedly not what you want? I don't understand how
this can work at all: if default parameter is never instantiated (which is
what my inderstanding of std::14.7.1/[1-2] suggests, but contrary to
compiler's opinion), there's not way to check if the member is present.
Otherwise, you'll have compile error.
- Volodya
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk