I\'ve been experimenting with the Curiously Recurring Template Pattern for a generic single-argument functor and have two implementations: one using a template template para
When the line
using Ftype = typename Functor::type;
is processed in the base class, the definition of Functor
is not available. Hence, you can't use Functor::type
.
One way to get around this limitation is to define a traits class.
// Declare a traits class.
template struct FunctorTraits;
template
class FunctorInterface_2 {
private:
const Functor &f_cref;
public:
// Use the traits class to define Ftype
using Ftype = typename FunctorTraits::type;
FunctorInterface_2() : f_cref(static_cast(*this)) {}
Ftype operator() ( Ftype val ) const { return f_cref(val); }
}; // FunctorInterface_2 (no type in Functor!)
// Forward declare Cube to specialize FunctorTraits
template struct Cube;
// Specialize FunctorTraits for Cube
template struct FunctorTraits>
{
using type = T;
};
template
struct Cube : public FunctorInterface_2> {
using type = T;
T operator() ( T val ) const { return val*val*val; }
}; // Cube
Working code: https://ideone.com/C1L4YW