Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 1999-12-01 14:20:00


> I have a problem with doing it through traits, because we would have to
> specialize, not only for every possible scalar and member pointer type, but
> every possible function pointer and member function pointer type. Is this
> even possible?

It seems pretty easy. Here's an example:

    #include <iostream>

    template<typename T>
    struct is_pointer_trait { enum { value = false }; };

    template<typename T>
    struct is_pointer_trait<T*> { enum { value = true }; };

    template<typename C, typename T>
    struct is_pointer_trait<T C::*> { enum { value = true }; };

    template<typename T>
    void tell_if_pointer(T)
    {
        std::cout << (is_pointer_trait<T>::value ? "yes\n" : "no\n");
    }

    static void f() { }
    static void g(int, int, void*, char) { }
    struct S1 { void h() { } };
    struct S2 { } j;
    
    int main()
    {
        tell_if_pointer(f); // yes
        tell_if_pointer(g); // yes
        tell_if_pointer(&S1::h); // yes
        tell_if_pointer(j); // no
    }

It would be hard to specialize specifically for all functions, but it's easy
to specialize for pointers, including function pointers.

    -- Darin


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