Boost logo

Boost :

From: Dirk Gerrits (dirkg_at_[hidden])
Date: 2002-05-14 07:35:51


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.
Assuming that I got the code right (please correct me if not), this must be
due
to a MSVC6 deficiency. I wanted to search the mailing list archive for a
solution but I didn't really know what to look for, so forgive me if this
has
been answered already. Here is the code plus MSVC6's complaint:

// Some code roughly copied from arg_tuple_size
// Just for testing

template<int num>
struct char_array
{
    char elements[num];
};

template<class R>
char_array<1> arity_helper(R (*)());

template<class R, class A1>
char_array<2> arity_helper(R (*)(A1));

template<class R, class A1, class A2>
char_array<3> arity_helper(R (*)(A1, A2));

// More overloads to be added, including boost::function, etc.

template<class F>
struct arity
{
    BOOST_STATIC_CONSTANT(int, value = (sizeof(arity_helper(
F(0) ).elements)-1));
};

template<bool Condition> struct truth { };
template<> struct truth<true> { typedef void* type; };

#define ONLY_CALLABLE_WHEN_ARITY_EQUALS(Functor, Arity) \
    truth<(arity<Functor>::value == Arity)>::type = 0

// Is this correct?
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() {};
int main()
{
    void (*nullary)() = &test;
    func(nullary);

    return 0;
}

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?

Dirk Gerrits


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