C1001 or crashable code when binding template function to functor
Dear all,
I use Boost.Function a lot and ran accross a crashable construction. I
can reproduce this with a simplified functor, which models
Boost.Function (up to 1%):
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
}
This crashes, because the bound address of the template isn't a valid
address. Assigning a normal function (i.e. no template) works as well
as initializing directly in constructor. If one leaves out the extra
dummy constructor argument (int i = 0), one gets C1001 on line 148
(workaround trick copied form Boost.Function).
We still use VStudio 2003 sp1 because it works mostly ok (except this
one). We did try VStudio 2005 (sp1), but there were more drawbacks
than advantages. VStudio 2005 choked on a correct C++ construct
(acknowledged by M$), the safe iterators and c-functions are
incompatible with Boost and very hard to suppress but the killer was
the long time Intellisense took to update our 102 project containing
solution (about 20 minutes and that each time).
Wkr,
me