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 

deduced Parameter does not match

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





PostPosted: Sat Dec 10, 2005 1:06 pm    Post subject: deduced Parameter does not match Reply with quote



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





PostPosted: Sun Dec 11, 2005 12:58 pm    Post subject: Re: deduced Parameter does not match Reply with quote



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:Sad*)(Parameter)

and

void Host:Sad*)(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
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.