Is the code below valid C++ according to the C++11 or C++14 standard?
#include <functional>
int ReturnInt()
{
return 5;
}
int main( int argc, char **argv )
{
std::function< void () > BoundType = ReturnInt;
return 0;
}
The code compiles fine with the latest cygwin versions of gcc (4.8.3) and clang (4.3.2) but not with Visual Studio 2013, Visual Studio November 2013 CTP or the Visual Studio 14 preview. It also compiles on all platforms if std::function is changed to boost::function.
I found this other stack overflow question that suggests it should work.
The code is undefined behavior in C++11, and ill-formed in C++14. C++14 adds this Remark to the specification of this constructor:
Callable is defined in [func.wrap.func]/p2:
For this INVOKE to be well formed, the return type of INVOKE without the
R
must be implicitly convertible toR
([func.require]/p2).In C++11 these statements were under a Requries clause, which means it is up to the client to get them right, and if the client fails, anything can happen, including successful compilation.
This was changed by LWG 2132.
std::function
in C++11 and 14 does not have the behavior you desire.It also fails to SFINAE detect bad overloads.
We can wrap it in a different type that both has the behavior you desire (
void
discarding return) and has SFINAE bad overload detection while we are at it as follows:It now compiles in C++14 (not in C++11, due to
wrap
:wrap
can be replaced at point of call with a copy of its own body, so...). Could probably reduce boilerplate by a bunch.It uses some C++14 features (move-into-lambda to be precise in
wrap
-- you can do away with that by adding more boilerplate).Not run yet.