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

Re: C1001 or crashable code when binding template function to functor by Alex

Alex
Wed Jun 27 05:49:23 CDT 2007

gast128@hotmail.com wrote:
[...]
> 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).

Just out of curiosity, why do you use separate types for
functor's constructors and member? I don't have VC++2003 to
check it, but you can try following workaround:

template <typename R, typename Arg1>
struct Functor
{
Functor(R (*pf)(Arg1)) : m_pf(pf) {}

R Invoke(Arg1 a1)
{
return m_pf(a1);
}

R (*m_pf)(Arg1);
};

Now template arguments match actual function type. This
construct is all over STL classes, so compiler should handle
it correctly.

Alex

Re: C1001 or crashable code when binding template function to functor by gast128

gast128
Wed Jun 27 08:35:24 CDT 2007

On 27 jun, 12:49, Alex Blekhman <x...@oohay.moc> wrote:
> gast...@hotmail.com wrote:
>
> [...]
>
> > 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).
>
> Just out of curiosity, why do you use separate types for
> functor's constructors and member? I don't have VC++2003 to
> check it, but you can try following workaround:
>
> template <typename R, typename Arg1>
> struct Functor
> {
> Functor(R (*pf)(Arg1)) : m_pf(pf) {}
>
> R Invoke(Arg1 a1)
> {
> return m_pf(a1);
> }
>
> R (*m_pf)(Arg1);
>
> };
>
> Now template arguments match actual function type. This
> construct is all over STL classes, so compiler should handle
> it correctly.
>
> Alex

You are right. My code is a simple model for Boost.Function which is
anvanced functor concept, capable of wrapping member functions, free
functions and functors themselves. Probably one ends up in casting the
way in and out when servicing so much different function like clients.

I got a crash in Boost.Function using above case (see Boost users
newsgroup, item 'Boost.Function and template function'), and by
creating a simplified case, I am pretty sure that it's a visual studio
2003 problem, instead of Boost.Function. But I need a M$ compiler guru
for the final answer...


Re: C1001 or crashable code when binding template function to functor by Alex

Alex
Wed Jun 27 11:00:45 CDT 2007

<gast128@hotmail.com> wrote:
> [...]
> I got a crash in Boost.Function using above case (see
> Boost users
> newsgroup, item 'Boost.Function and template function'),
> and by
> creating a simplified case, I am pretty sure that it's a
> visual studio
> 2003 problem, instead of Boost.Function. But I need a M$
> compiler guru
> for the final answer...

You don't need a guru for this. You discovered this crash by
yourself. Moreover, later version of the compiler doesn't
exhibit the problem. So, most likely you'll be advised to
upgrade the comopiler.

There is somewhat similar bug in VC++2005:

"Compiler is unable to call a ... function with a pointer to
an instance of a function template"
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=274222

A possible workaround is mentioned in the bug report, that
is to take a copy of function pointer before using it. Maybe
it'll help in youe case, too.

Alex