By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

can i automatically detect existence of a class member ?

P: n/a
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 Tstruct can_insert : public std::false_type{};
and explicitly write
template<typename Tstruct 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 = voidstruct can_insert :
public std::false_type{};
and
template<typename T>
struct can_insert < enable_if<typename T::insert_tag>::typestruct
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
Nov 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 Tstruct can_insert : public std::false_type{};
and explicitly write
template<typename Tstruct 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

Nov 8 '08 #2

P: n/a
On Nov 8, 1:16 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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 Tstruct can_insert : public std::false_type{};
and explicitly write
template<typename Tstruct 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
Nov 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.