|
Boost : |
From: Dirk Gerrits (dirkg_at_[hidden])
Date: 2002-05-14 12:45:50
This is excellent! Thanks a lot. One thing though, why make the
unspecialized version of arity_helper return yes_type?
Dirk Gerrits
----- Original Message -----
From: "Yitzhak Sapir" <yitzhaks_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, 14 May, 2002 20:15
Subject: RE: [boost] Is there a workaround for this MSVC6 deficiency?
> I got it to produce the following message:
> aritytester.cpp
> C:\AC_SERVER_III\aritytester\aritytester.cpp(77) : error C2893: Failed to
specialize function template 'void __cdecl func(F,struct
arity_was_not_valid)'
> With the following template arguments:
> 'void (__cdecl *)(int)'
> Error executing cl.exe.
>
...
>
> using the following code:
>
> template<int ArityToCheck> struct arity_checker
> {
> static yes_type arity_helper(...);
> };
>
> template<> struct arity_checker<0>
> {
> template <class R> static yes_type arity_helper(R (*)());
> static no_type arity_helper(...);
> };
>
> // More overloads to be added, including boost::function, etc.
>
> struct arity_was_not_valid
> {
> arity_was_not_valid(int value) {};
> };
>
> template<bool Condition> struct arity_valid;
> template<> struct arity_valid<true> { typedef arity_was_not_valid type; };
>
> template<class F, int Arity> struct is_callable
> {
> enum { arity_ok =
> (sizeof(arity_checker<Arity>::arity_helper(F(0))) == sizeof(yes_type)) };
> typedef arity_valid<arity_ok>::type arity_was_not_valid_type;
> };
>
> #define ONLY_CALLABLE_WHEN_ARITY_EQUALS(Functor, Arity) \
> is_callable<Functor, Arity>::arity_was_not_valid_type = 0
>
> template<class F>
> void func(F f, ONLY_CALLABLE_WHEN_ARITY_EQUALS(F, 0)) // 'type' : is not a
member of 'truth<0>'
> {
> std::cout << typeid(F).name() << ": 0 parameters\n";
> }
>
> void test() {};
> void test2(int) {};
> int main()
> {
> func(test);
> func(test2);
> return 0;
> }
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk