Just Let It Flow

August 24, 2009

When Enabling stdcall Put Me in a Bind

Filed under: Wow, that was stupid — adeyblue @ 5:16 pm

For the first time the other day, I was in a position where I needed to use boost::bind with a plain Windows function. Having used COM objects with shared_ptr in the past, I remembered that the BOOST_MEM_FN_ENABLE_STDCALL define is required to get mem_fn to work with stdcall functions. Before jumping in with bind, I checked if there was something similar for it as they’re both concerned with calling conventions and function pointers. This turned up BOOST_BIND_ENABLE_STDCALL which I dutifully defined and went on my way.

#include <boost/bind.hpp>
boost::bind(&ShellExecute, ...);

And then I compiled:

bind\bind_cc.hpp(22) : error C2995: 'boost::_bi::bind_t<R,R(__stdcall *)(void),boost::_bi::list0> boost::bind(R (__stdcall *)(void))' : function template has already been defined
        bind\bind_cc.hpp(17) : see declaration of 'boost::bind'
bind\bind_cc.hpp(31) : error C2995: 'boost::_bi::bind_t<R,R(__stdcall *)(B1),_bi::list_av_1<A1>::type> boost::bind(R (__stdcall *)(B1),A1)' : function template has already been defined
        bind\bind_cc.hpp(26) : see declaration of 'boost::bind'

Where did the duplicate definitions come from? The pre-processed output showed the normal definitions and then the explicit __stdcall ones, so I rebuilt the project in case it was just one of those quirks. The result was identical. As they were apparently duplicate definitions, I removed the define and tried again… and it compiled fine.

Then the facepalm moment. I’d changed the default calling convention of the project to stdcall earlier, which made the normal bind definitions stdcall to begin with. Sometimes I really am a bit of a numpty.

Moral of the story:
If you’re compiling with the /Gz flag in Visual Studio, don’t define BOOST_BIND_ENABLE_STDCALL because bind is already stdcall compliant.

1 Comment »

  1. Thank you. Same mistake, but I hadn’t followed the logic chain as well as you. In my case, I’ve inherited someone else’s project files, and I never considered that they might change the default calling convention… *argh*.

    Comment by Joe Marley — January 24, 2013 @ 8:28 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress