Boost logo

Boost :

Subject: Re: [boost] C++03 / C++11 compatibility question for compiled libraries
From: Peter Dimov (lists_at_[hidden])
Date: 2018-02-10 15:43:12


John Maddock wrote:
> Trying to get back to the case in point... is there any reason not to
> support both in the binary library?
>
> ie
>
> virtual int operator()( boost::function<int ()> const& F ) = 0;
> virtual int operator()( std::function<int ()> const& F ) = 0;

If you make the function virtual that just makes the problem worse. A
non-virtual function will fail to link if you pass boost::function from one
side but take std::function from the other. A virtual will silently call the
std::function-taking override, passing it a boost::function. Much joy
results.

All this really makes no sense at all; boost::function should be used.

But, if you really wanted to know, libstdc++ in a situation like the above
(when the function has to be virtual because it's f.ex. specified that way
by the standard) does something like the following:

#if cpp11

virtual int __f( boost::function<int ()> const& F ) = 0;
virtual int f( std::function<int ()> const& F ) = 0;

#else

virtual int f( boost::function<int ()> const& F ) = 0;
virtual int __f( std::function<int ()> const& F ) = 0;

#endif

except of course we don't have std::function in C++03 mode, so even
declaring __f will be a problem.

If you run the variants in your head, C++03 exe with C++11 lib works, but
C++11 exe with C++03 lib cannot. The best you can do in __f is crash.


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