[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

can i automatically detect existence of a class member ?

abir

11/8/2008 8:09:00 AM

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
4 Answers

Kai-Uwe Bux

11/8/2008 8:17:00 AM

0

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

abir

11/8/2008 12:39:00 PM

0

On Nov 8, 1:16 pm, Kai-Uwe Bux <jkherci...@gmx.net> 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

John Hanley

1/31/2012 10:44:00 AM

0

On Mon, 30 Jan 2012 16:53:08 +0000 (UTC), 2875 Dead <dead@gone.com>
wrote:

>On Mon, 30 Jan 2012 05:57:02 -0500, NoBody wrote:
>
>> On Sat, 28 Jan 2012 04:41:45 +0000 (UTC), 2875 Dead <dead@gone.com>
>> wrote:
>>
>>>On Fri, 27 Jan 2012 21:38:07 -0700, Yoorghis wrote:
>>>
>>>> On Fri, 27 Jan 2012 17:53:24 +0000 (UTC), 2875 Dead <dead@gone.com>
>>>> wrote:
>>>>
>>>>>> It's amazing how Zippy / Yorgort flees every time he looks bad.
>>>>>
>>>>>
>>>>>Sometimes the smartest thing I can do is let you have the last word.
>>>>>You do such a good job of blowing yourself up.
>>>>
>>>> His only qualification as a poster is to stamp his foot and screech:
>>>> "Is not, is not, is not"
>>>>
>>>> I'm not posting anything remarkably hard to find, nothing that can't
>>>> be instantly googled and backed by thousands of separate sites---yet
>>>> his replies are limited "is not".
>>>>
>>>> He even disavowed a standard figure of debt left by each president to
>>>> the next because I couldn't prove it was "exact"
>>>>
>>>> His last foot stomping was to say the (nearly) $16 Trillion debt was
>>>> almost 85% Republican caused because the "figures are out of date"
>>>>
>>>> He can't figure out that adding to the ONGOING disaster---the rising
>>>> costs are ADDED to that percentage.
>>>>
>>>> His side argues that the Carter Malaise "caused" various problems in
>>>> the Reagan Administration---as a means to shift blame from Reagan.
>>>>
>>>> Yet here he is arguing that Obama MUST take all responsibility for
>>>> debt from Jan 20th 2009 on.
>>>>
>>>> He's a net troll o' the 4th kind.
>>>
>>>Whaddya expect? He's just a Cason sockpuppet.
>>>
>>>
>>>
>> How cute, a sockpuppet claiming I'm a sockpuppet...
>
>Try to say something original, bubbles...
>

Translation: Zippo / Yorgort just got caught.

MikeD

1/31/2012 10:45:00 AM

0


On Mon, 30 Jan 2012 05:56:16 -0500, NoBody <NoBody@nowhere.com> wrote:

>On Fri, 27 Jan 2012 21:38:07 -0700, Yoorghis@Jurgis.net wrote:
>
>>On Fri, 27 Jan 2012 17:53:24 +0000 (UTC), 2875 Dead <dead@gone.com>
>>wrote:
>>
>>>> It's amazing how Zippy / Yorgort flees every time he looks bad.
>>>
>>>
>>>Sometimes the smartest thing I can do is let you have the last word. You
>>>do such a good job of blowing yourself up.
>>
>>His only qualification as a poster is to stamp his foot and screech:
>>"Is not, is not, is not"
>
>If you'd actually provide something current that is not plagiarized,
>you *could* try to make that point. As it stands now, you just look
>like the lying fool you are.
>
>>
>>I'm not posting anything remarkably hard to find, nothing that can't
>>be instantly googled and backed by thousands of separate sites---yet
>>his replies are limited "is not".
>
>Many of your posts contain out of date data to make your "point". When
>I provide the correct data, you flee and just repeat your claim later.
>Several of your quotations have not appeared in google searches. In
>short, if you can't be an adult and not blatantly steal other's work
>(by not citing your source you are doing so), there isn't really much
>to debate.
>
>>
>>He even disavowed a standard figure of debt left by each president to
>>the next because I couldn't prove it was "exact"
>
>Uh huh, being off by 100% for you apparently is good enough.
>
>>
>>His last foot stomping was to say the (nearly) $16 Trillion debt was
>>almost 85% Republican caused because the "figures are out of date"
>
>Actually no. I'm still waiting for you to provide anything credible
>for your 85% number. The cite you provide says nothing about that.
>What I do take issue with is your debt numbers that show Obama's debt
>wrong by half.
>
>>
>>He can't figure out that adding to the ONGOING disaster---the rising
>>costs are ADDED to that percentage.
>
>You can't figure out that the person signing the bills is responsible
>for the debt incurred by those bills.
>
>>
>>His side argues that the Carter Malaise "caused" various problems in
>>the Reagan Administration---as a means to shift blame from Reagan.
>>
>
>What are you talking about?
>
>>Yet here he is arguing that Obama MUST take all responsibility for
>>debt from Jan 20th 2009 on.
>
>Did he sign the bills? Let's use your logic shall we: the dot com
>bubble burst at the end of Clinton's Presidency. Bush's spending is
>Clinton's fault (again using your *logic*).
>
>>
>>He's a net troll o' the 4th kind.
>
>BWWWAAAHAAA! Irony anyone?

And Yorgort / Zippo runs away with his tail tucked between his legs
again...