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 

Template specialization issue

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





PostPosted: Wed Oct 12, 2005 3:10 pm    Post subject: Template specialization issue Reply with quote



The fully templated function call is undefined:

template<class COMPONENT, class SUBSYSTEM>
COMPONENT::Result perform_tests( SUBSYSTEM & s );

or

template<class COMPONENT>
class Traits
{
template<class SUBSYSTEM>
COMPONENT::Result perform_tests( SUBSYSTEM & s );
};

'perform_tests' is to call some function on 's', based on partial
template specialization, ala:

template<class SUBSYSTEM>
Component::Result perform_tests<Component>( SUBSYSTEM & s )
{
return s.perform_Component_tests();
}

or

template<class SUBSYSTEM>
Component::Result Traits<Component>::perform_tests( SUBSYSTEM & s )
{
return s.perform_Component_tests();
}

I need a compile-failure when the 'perform_Component_tests()' doesn't
exist for the SUBSYSTEM type. And 'perform_Component_tests()' can't be
templated since it's a virtual function generated from an IDL file.

The call syntax is a template function in a template class similiar to:

template<class SUBSYSTEM>
struct Caller
{
Caller( SUBSYSTEM & s ) : s_(s) ;

template<class COMPONENT>
void operator()()
{
COMPONENT::Result = perform_tests<COMPONENT>(s);
or
COMPONENT::Result = Traits<COMPONENT>::perform_tests(s_);
}

SUBSYSTEM & s_;
};

Is there a better (standard) way to do this? Which one of my examples,
if either of them, is a standard way to do this?


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

Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 12, 2005 10:42 pm    Post subject: Re: Template specialization issue Reply with quote



[email]lindahlb (AT) hotmail (DOT) com[/email] wrote:
Quote:
The fully templated function call is undefined:

template<class COMPONENT, class SUBSYSTEM
COMPONENT::Result perform_tests( SUBSYSTEM & s );

should probably be

template typename COMPONENT::Result perform_tests( SUBSYSTEM & s );

Quote:
or

template<class COMPONENT
class Traits
{
template COMPONENT::Result perform_tests( SUBSYSTEM & s );

And here as well

template typename COMPONENT::Result perform_tests( SUBSYSTEM & s );


Quote:
};

'perform_tests' is to call some function on 's', based on partial
template specialization, ala:

template<class SUBSYSTEM
Component::Result perform_tests {
return s.perform_Component_tests();
}

Assuming 'Component' is a concrete type defined at this point, this
definition should be a separate template, not a partial specialisation of
the other one. There are no partial specialisations of function templates
in C++. Therefore the syntax should be

template<class SUBSYSTEM>
Component::Result perform_tests(SUBSYSTEM & s)
{
return s.perform_Component_tests();
}

Quote:
or

template<class SUBSYSTEM
Component::Result Traits {
return s.perform_Component_tests();
}

That's probably possible because you're defining a member template of
a class template specialisation.

Quote:
I need a compile-failure when the 'perform_Component_tests()' doesn't
exist for the SUBSYSTEM type. And 'perform_Component_tests()' can't be
templated since it's a virtual function generated from an IDL file.

The call syntax is a template function in a template class similiar to:

template<class SUBSYSTEM
struct Caller
{
Caller( SUBSYSTEM & s ) : s_(s) ;

template void operator()()
{
COMPONENT::Result = perform_tests or
COMPONENT::Result = Traits<COMPONENT>::perform_tests(s_);
}

SUBSYSTEM & s_;
};

Is there a better (standard) way to do this? Which one of my examples,
if either of them, is a standard way to do this?

I think you need to create a specialisation for 'Caller' that would have
a different implementation of 'operator()' depending on COMPONENT having
or not having the member function you look for. See Boost::has_member
(IIRC) template for example. Something in line with

template<class SUBSYSTEM,
bool usemember = has_member
struct Caller; // undefined
template<class SUBSYSTEM> struct Caller<SUBSYSTEM, true> {
...
void operator()() {
// has member
s.perform_tests();
}
};

template<class SUBSYSTEM> struct Caller<SUBSYSTEM, false> {
...
void operator()() {
// has no member
perform_tests(s);
}
};

V

[ 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.