 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Arne Adams Guest
|
Posted: Sat Dec 10, 2005 1:06 pm Post subject: deduced Parameter does not match |
|
|
template<class Parameter, class Host> void Configure(Parameter, void
(Host::*)(Parameter))
{/* do something useful*/}
// something did not match - generate a compiler error that might clarify
what went wrong
template<class Parameter, class MemFun> void Configure(Parameter p, MemFun
f)
{
CheckParameter(p,f);
}
struct
ERROR_ERROR_ERROR_the_type_of_the_parameter_for_your_member_function_does_not_match_the_first_parameter_of_Configure_ERROR_ERROR_END;
template<class WrongParameter, class NeededParameter, class Host>
void CheckParameter(WrongParameter, void (Host::*)(NeededParameter))
{
ERROR_ERROR_ERROR_the_type_of_the_parameter_for_your_member_function_does_not_match_the_first_parameter_of_Configure_ERROR_ERROR_END
dontCompileThis;
}
struct Horst
{
void Fifi(Horst&);
};
int main(int argc, char* arg[])
{
Horst horst;
Configure(horst,&Horst::Fifi);/* Compiler error mentioning
ERROR_ERROR_ERROR_the_type_of_the_parameter_for_your_member_function_does_not_match_the_first_parameter_of_Configure_ERROR_ERROR_END;*/
return 0;
}
Both GCC 3.3 and MSVC++ 7.1 seem to agree here - the problem goes away when
Fifi does accept Horst per copy and as well when Configure<Horst&> is
called.
Why is Parameter not deduced as Host& here - is this behaviour conforming?
Kind Regards,
Arne
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
benben Guest
|
Posted: Sun Dec 11, 2005 12:58 pm Post subject: Re: deduced Parameter does not match |
|
|
| Quote: | [demostration code snipped]
Both GCC 3.3 and MSVC++ 7.1 seem to agree here - the problem goes away when
Fifi does accept Horst per copy and as well when Configure<Horst&> is
called.
Why is Parameter not deduced as Host& here - is this behaviour conforming?
|
Because
void Host: *)(Parameter)
and
void Host: *)(Parameter&)
are different types. Allowing they to be treated as the same type will
have a subtle effect where discriminating reference type does matter.
To work around you can do the following:
// start with some helper templates:
template <typename T> struct get_base_type{
typedef T type;
};
tempatle <typename T> struct get_base_type<get_base_type
typedef T type;
};
class Yes{}; class No{};
template <typename T1, typename T2> struct is_same_type{
typedef No result;
};
template <typename T> struct is_same_type<T, T>{
typedef Yes result;
};
template <typename T> struct static_assert{
typedef typename T::ERROR_ERROR_BLAH_BLAH_BLAH error;
typedef No type;
};
template <> struct static_assert<Yes>{
typedef Yes type;
};
// then the function template:
template<class Param1, class Param2, class Host>
void configure(
Param1,
void(Host::*)(Param2))
{
typedef typename get_base_type<Param2>::type P2;
typedef typename is_same_type<Param1, P2>::type R;
typedef sttic_assert<R>::type Assertion;
// ...
}
| Quote: |
Kind Regards,
Arne
|
[ 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
|
|