Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2002-05-14 13:15:57


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.

Line 77 is the line in which the bad call is made in my case. I'm not sure the compiler is making all the right decisions though to get to this result (which seems to be what we would want). It would be nice if we could state what arity we wanted, in the error message, but either MSVC or me seems to be having trouble doing that part.

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;
}

> From: Dirk Gerrits
>
> Douglas Gregor was kind enough to show me a neat generic
> programming trick
> which
> could potentially lead to a much improved Boost.Function in
> the future:
>
> http://aspn.activestate.com/ASPN/Mail/Message/boost/1201004
>
> However, when I tried something similar out in my own code, MSVC6
> complained.
[...]
> been answered already. Here is the code plus MSVC6's complaint:
>
> // Some code roughly copied from arg_tuple_size
> // Just for testing
>
[...]
>
> Even though the template argument to truth should be true,
> MSVC6 complaints
> that type is not a member of truth<false>. Is there a known
> workaround for
> this? Or is MSVC6 barred from the potential Boost.Function
> improvement?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk