can i automatically detect existence of a class member ?

Discussion in 'C++' started by abir, Nov 8, 2008.

  1. abir

    abir Guest

    Hi,
    i need to work on a certain types of sequences which has a specific
    property.
    some of the properties i can deduce based on a few typenames, but
    others i can't

    eg i want to know a class which supports insert.
    so i have
    template<typename T> struct can_insert : public std::false_type{};
    and explicitly write
    template<typename T> struct can_insert<std::vector<T>> : public
    std::true_type{}; etc

    for my own classes i can introduce a tag for the whole category
    instead of specializing for each one
    eg
    template<typename T,typename Enabler = void> struct can_insert :
    public std::false_type{};
    and
    template<typename T>
    struct can_insert < enable_if<typename T::insert_tag>::type> struct
    can_insert : public std::true_type{};

    where for my class i define the tag
    struct mytype{
    typedef std::true_type insert_tag ;

    }
    but i can't have a tag for all of the classes. so i am looking for
    something like
    template<typename T>
    struct can_insert<T, enable_if<has_member<T,T::*insert>::type > :
    public std::true_type{};

    so can i detect if a member (function or variable) of some specific
    signature is present in the class?
    (something like __if_exists in MSVC ? )

    thanks
    abir
    abir, Nov 8, 2008
    #1
    1. Advertising

  2. abir

    Kai-Uwe Bux Guest

    abir wrote:

    > Hi,
    > i need to work on a certain types of sequences which has a specific
    > property.
    > some of the properties i can deduce based on a few typenames, but
    > others i can't
    >
    > eg i want to know a class which supports insert.
    > so i have
    > template<typename T> struct can_insert : public std::false_type{};
    > and explicitly write
    > template<typename T> struct can_insert<std::vector<T>> : public
    > std::true_type{}; etc

    [snip]

    Maybe, the following helps. It deals with swap(), but it is straight forward
    to addapt the example to insert().

    template < typename T >
    class has_swap {
    /*
    stolen from Rani Sharoni, who attributes this to
    Richard Smith and also Artem Livshits
    */

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

    template < typename S, void ( S::* ) ( S & ) >
    struct dummy {};

    template < typename S >
    static
    yes check ( dummy< S, &S::swap > * );

    template < typename S >
    static
    no check ( ... );

    public:

    static bool const value = sizeof( check<T>(0) ) == sizeof( yes );

    }; // has_swap


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 8, 2008
    #2
    1. Advertising

  3. abir

    abir Guest

    On Nov 8, 1:16 pm, Kai-Uwe Bux <> wrote:
    > abir wrote:
    > > Hi,
    > > i need to work on a certain types of sequences which has a specific
    > > property.
    > > some of the properties i can deduce based on a few typenames, but
    > > others i can't

    >
    > > eg i want to know a class which supports insert.
    > > so i have
    > > template<typename T> struct can_insert : public std::false_type{};
    > > and explicitly write
    > > template<typename T> struct can_insert<std::vector<T>> : public
    > > std::true_type{}; etc

    >
    > [snip]
    >
    > Maybe, the following helps. It deals with swap(), but it is straight forward
    > to addapt the example to insert().
    >
    > template < typename T >
    > class has_swap {
    > /*
    > stolen from Rani Sharoni, who attributes this to
    > Richard Smith and also Artem Livshits
    > */
    >
    > typedef char (&no) [1];
    > typedef char (&yes) [2];
    >
    > template < typename S, void ( S::* ) ( S & ) >
    > struct dummy {};
    >
    > template < typename S >
    > static
    > yes check ( dummy< S, &S::swap > * );
    >
    > template < typename S >
    > static
    > no check ( ... );
    >
    > public:
    >
    > static bool const value = sizeof( check<T>(0) ) == sizeof( yes );
    >
    > }; // has_swap
    >
    > Best
    >
    > Kai-Uwe Bux


    Thanks for reply
    It looks cool & I used it successfully for my purpose
    However can I use it to detect availability of a particular
    constructor in the class.
    Something like the one below doesn't work

    struct move_ctor{};
    template<typename T>
    class has_move{
    typedef char (&no) [1];
    typedef char (&yes) [2];

    template<typename S, void ( S::* ) ( S & ,move_ctor)>
    struct dummy {};

    template<typename S>
    static yes check ( dummy< S, &S::S> * );
    template < typename S >
    static no check ( ... );
    public:
    const static bool value = sizeof( check<T>(0) ) == sizeof( yes );
    };

    It complains that i cant take address of the constructor, which is
    legitimate.
    Any alternative for this?

    Thanks again
    abir
    abir, Nov 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,716
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ivan
    Replies:
    2
    Views:
    332
    Frank Schmitt
    Sep 4, 2003
  3. Siemel Naran
    Replies:
    4
    Views:
    789
    Micah Cowan
    Jan 12, 2005
  4. lihua
    Replies:
    19
    Views:
    912
    CBFalconer
    Jul 7, 2005
  5. usenet only_tech_talk

    check for existence of static member variable

    usenet only_tech_talk, Mar 6, 2010, in forum: C++
    Replies:
    1
    Views:
    542
    usenet only_tech_talk
    Mar 6, 2010
Loading...

Share This Page