can i automatically detect existence of a class member ?

A

abir

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
 
K

Kai-Uwe Bux

abir said:
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
 
A

abir

abir said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top