我试图在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_;
};
Run Code Online (Sandbox Code Playgroud)
我基本上试图将所有sfinae锅炉板移动到一个基类中,所以我不需要为我创建的每个操作重复它(目前每个操作有6个不同的调用,并且有大约50个操作,所以有相当的使用enable_if进行了大量的重复.
我已经尝试了一个依赖于重载的解决方案但是其中一个可以传递的类型是任何可调用的东西(这可以是来自C++ 03或C++ 0x lambda的常规仿函数),我绑定到std: :不幸的是,函数来自std :: function的开销虽然非常小,但实际上在这个应用程序中有所不同.
有没有办法解决我目前的问题,还是有更好的解决方案来解决这个问题?
谢谢.