Boost.Function and template function

Dear all, I get a crash when I try to wrap a template function with Boost.Function. I use VStudio 2003 sp1. But maybe the used syntax is wrong: #include <boost/function.hpp> struct SomeClass { }; template <typename T> bool func_template(T* p) { return (p != NULL); } void example1() { bool (*pf) (SomeClass*) = &func_template<SomeClass>; boost::function<bool (SomeClass*)> fc1 = &func_template<SomeClass>; boost::function1<bool, SomeClass*> fc2 = &func_template<SomeClass>; boost::function<bool (SomeClass*)> fc3 = pf; fc1(NULL); //crashes fc2(NULL); //crashes also fc3(NULL); //ok } Wkr, me

Since no one react, I tried to reverse engineer Boost.Function and pin down the problem: template <typename R, typename Arg1> struct Functor { template <typename F> Functor(F f, int i = 0) : m_pf(0) { m_pf = reinterpret_cast<Pf>(f); } template <typename T> R Invoke(T t) { typedef R (*Pf1)(Arg1); return (*(Pf1)m_pf)(t); } typedef void (*Pf)(void); Pf m_pf; }; template <typename T> bool func_template(T* p) { return p != 0; } void TestBugFunctor() { Functor<bool, int*> fc2 = &func_template<int>; fc2.Invoke((int*)0); //crash } The trick with the extra dummy constructor argument is copied form boost function header files. Without it one gets an ICE, but with it, VStudio 2003 sp1 generates a suspectable call. I will crosspost this in a M$ group and find out if there are interesting reactions.

On Jun 26, 2007, at 5:05 PM, gast128 wrote:
Since no one react, I tried to reverse engineer Boost.Function and pin down the problem:
template <typename R, typename Arg1> struct Functor { template <typename F> Functor(F f, int i = 0) : m_pf(0) { m_pf = reinterpret_cast<Pf>(f); }
template <typename T> R Invoke(T t) { typedef R (*Pf1)(Arg1); return (*(Pf1)m_pf)(t); }
typedef void (*Pf)(void); Pf m_pf; };
template <typename T> bool func_template(T* p) { return p != 0; }
void TestBugFunctor() { Functor<bool, int*> fc2 = &func_template<int>; fc2.Invoke((int*)0); //crash }
The trick with the extra dummy constructor argument is copied form boost function header files. Without it one gets an ICE, but with it, VStudio 2003 sp1 generates a suspectable call.
Oh, that's bad. I don't have this compiler on hand to work with... if someone has a workaround, I would gladly apply it to Boost.Function. - Doug
participants (2)
-
Doug Gregor
-
gast128