Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can i automatically detect existence of a class member ?

Reply
Thread Tools

can i automatically detect existence of a class member ?

 
 
abir
Guest
Posts: n/a
 
      11-08-2008
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-08-2008
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

 
Reply With Quote
 
 
 
 
abir
Guest
Posts: n/a
 
      11-08-2008
On Nov 8, 1:16 pm, Kai-Uwe Bux <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check for existence of static member variable usenet only_tech_talk C++ 1 03-06-2010 04:31 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Member function existence queries... Ivan C++ 2 09-04-2003 11:12 AM



Advertisments