我正在尝试自定义std::function
并从以下代码开始:
#include <functional>
template <typename>
struct my_function;
template <typename R, typename... Args>
struct my_function<R(Args...)> : std::function<R(Args...)> {
using base = std::function<R(Args...)>;
using base::function;
};
int main() {
my_function<int(int)> f = nullptr;
}
我期望从中std::function
获取std::nullptr_t
对象的构造函数由my_function
. 因此,代码应该可以正常编译。虽然 GCC 5.2 (C++14) 在编译代码时没有问题,但 clang 3.6 (C++14) 产生了以下非常令人困惑的错误消息:
n file included from main.cpp:1:
/usr/include/c++/v1/functional:1454:41: error: no type named 'type' in 'std::__1::enable_if<false, void>'; 'enable_if' cannot be used to disable this declaration
__callable<_Fp>::value &&
^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:10:15: note: in instantiation of member function 'std::__1::function<int (int)>::function' requested here
using base::function;
^
main.cpp:14:25: note: while substituting deduced template arguments into function template 'my_function' [with _Fp = nullptr_t]
my_function<int(int)> f = nullptr;
^
1 error generated.
这是一个clang错误还是我的代码不正确?如果(非常幸运)后者是真的,我应该如何修复代码?