#include <functional>
#include <sys/types.h>
#include <sys/socket.h>
std::function<decltype(::bind)> mockbind = ::bind;
int main()
{
}
The code above works on most of the platforms I compile on. But on ubuntu 14.04 using g++-7 I get an error:
X.cpp:7:65: error: variable ‘std::function<int(int, const sockaddr*, unsigned int) noexcept> mockbind’ has initializer but incomplete type
std::function<int(int, const sockaddr*, unsigned int) noexcept> mockbind = ::bind;
^~~~~~~~
Now if I manually go and change the type of mockbind
std::function<int(int, const sockaddr*, unsigned int) noexcept> mockbind = ::bind;
As expected I get the same error:
Now if I remove the noexcept
std::function<int(int, const sockaddr*, unsigned int)> mockbind = ::bind;
It compiles as expected.
So the question is can I apply some template code to remove the noexcept
from the type returned by decltype
and make it work as expected.
A simple class specialization trick should work:
You could somewhat easily extend it to remove
noexcept
from [member] function pointers, that's left as an excercise to the reader.Also you could comment out
using type = T;
if you wish to get a compile-time error if there is nonoexcept
instead of leaving the type unchanged.