 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lindahlb@hotmail.com Guest
|
Posted: Wed Oct 12, 2005 3:10 pm Post subject: Template specialization issue |
|
|
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
|
Posted: Wed Oct 12, 2005 10:42 pm Post subject: Re: Template specialization issue |
|
|
[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 |
|
 |
|
|
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
|
|