我正在尝试在 CRTP 基类中的成员函数的后期指定返回中使用 decltype 并且它的错误是:invalid use of incomplete type const struct AnyOp<main()::<lambda(int)> >
。
template<class Op>
struct Operation
{
template<class Foo>
auto operator()(const Foo &foo) const ->
typename std::enable_if<is_foo<Foo>::value,
decltype(static_cast<const Op*>(nullptr)->call_with_foo(foo))>::type
{
return static_cast<const Op*>(this)->call_with_foo(foo);
}
};
template<class Functor>
struct AnyOp : Operation<AnyOp<Functor> >
{
explicit AnyOp(Functor func) : func_(func) {}
template<class Foo>
bool call_with_foo(const Foo &foo) const
{
//do whatever
}
private:
Functor func_;
};
我基本上是在尝试将所有 sfinae 样板移动到一个基类中,所以我不需要为我创建的每个操作重复它(目前每个操作有 6 个不同的调用,并且有大约 50 个操作,所以有很多enable_if 有很多重复)。
我尝试了一个依赖于重载的解决方案,但是可以传递的类型之一是我绑定到 std 的任何可调用的类型(这可以是来自 C++03 的常规函子或 C++0x lambda): :function,不幸的是,std::function 的开销虽然非常小,但实际上对这个应用程序产生了影响。
有没有办法解决我目前拥有的问题,或者是否有更好的解决方案一起解决这个问题?
谢谢。