C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q for C++ template Guru: how to use SFINAE to determine meta

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
G. Wang
Guest





PostPosted: Wed Apr 21, 2004 9:21 am    Post subject: Q for C++ template Guru: how to use SFINAE to determine meta Reply with quote



Hi,

I tried but failed to find a solution to a real problem that I
experienced. I am hoping that your gurus out there can help me out or
point me to the right direction.

I striped down the real code to the barebone to demonstrate the
problem. I know the posting is still long, so I appreciate your time
and efforts. You can find the code at the end of the post.

Here is the situation. I have a meta class A that has a member
variable whose type is an instance of a meta class B determined by a
meta function ( get_B in the code ). Any instance of meta class B
logically owns a value whose type is an instance of meta class A. If B
holds the value by pointer as demonstrated in Line B, everything works
fine, except that I pay the performance penalty of dynamic memory
allocation.

I don't want to pay the performance penalty, so I want to hold the
value inside B whenever possible, as demonstrated by line A. If I do
so, I will have compile time error. The instantiation of A<0> requires
the compiler to instantiate A<1>, which requires the instantiation of
B_Hold_A_ByValue<1>, which requires the full definition of A<1>, which
we donot have because we are currently instantiate A<1>!.

Here is my question. How can I apply SFINAE principle to determine
case-by-case on when B should hold the value by pointer, when directly
by value? If SFINAE won't help me here, what technique would you
recommend for the situation?

I tried various things on VC 7.1. All failed. I donot want to claim
that the compiler does not support SFINAE properly without hearing
from your guys first.

Thanks a lot and have a good day. Hope to hear from you soon.

===========================================================================

template<int N>
struct determine_N
{
enum { value = 1 };
};

template<int N>
struct get_B;

template< int N >
struct A
{
enum { value = determine_N<N>::value };

typename get_B<value>::type content;
};

template<int N>
struct B_Hold_A_ByValue
{
struct self {};

A<N> m_content;

A<N>& Content()
{
return m_content;
}
};

template<int N>
struct B_Hold_A_ByPointer
{
struct self {};

std::auto_ptr< A m_pContent;

A<N>& Content()
{
if( m_pContent.get() == NULL )
{
m_pContent = std::auto_ptr< A( new A<N> );
}

return *m_pContent;
}
};


template<int N>
struct get_B
{
// how to use SFINAE to determine if "type" should be
// B_Hold_A_ByValue<N> or
// B_Hold_A_ByPointer<N>?

// typedef B_Hold_A_ByValue<N> type; // <----- line A
typedef B_Hold_A_ByPointer };

void main()
{
A<0> a;
}

============================================================================

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Fri Apr 23, 2004 12:25 am    Post subject: Re: Q for C++ template Guru: how to use SFINAE to determine Reply with quote



[email]visionsoft (AT) hotmail (DOT) com[/email] (G. Wang) wrote in message news:<59199ce9.0404201501.1566e36c (AT) posting (DOT) google.com>...
Quote:
Hi,

I tried but failed to find a solution to a real problem that I
experienced. I am hoping that your gurus out there can help me out or
point me to the right direction.

I striped down the real code to the barebone to demonstrate the
problem.

I'm afraid you stripped too much. I could even compile the attached
code in VC6, if I ordered it correctly.

Besides, I fail to understand your goal. Meta-programming is especially
vulnerable to a lack of understanding of the problem, and I fear you
don't understand your problem either. I think this because your post
is talking about solutions, but doesn't explain what problem it's
trying to solve.

SFINAE is primarily used to eliminate functions from overload sets.
This can be used for static deduction, as sizeof( foo(X) ) will do
overload resolution on foo(X). The trick used is to let one version
return char, another class big{char[2]}, and to use SFINAE to
eliminate one of the two for certain template parameters. As you
don't give us a clue as to which template parameters to use,
we can't give you an exact solution.

Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.