Category : functor

The following code snippet doesn’t compile because of the absence of copy ctor. template <typename Func> void print(Func f) { f(); } struct abc { abc() = default; abc(const abc&) = delete; abc& operator=(const abc&) = delete; void operator()() { std::cout << "f" << std::endl; } }; int main() { abc s; print(s); return 0; ..

Read more

I’d like to have something like this: struct Foo { template <typename T> void operator()(T i) const { /* … */ } template <typename T> auto To() const { // Doesn’t work! return this->*static_cast<void (Foo::*)(int) const>(&Foo::operator()); } }; Basically, I’d like to obtain a functor from a member function pointer. The above code would have ..

Read more

I’d like to have something like this: struct Foo { template <typename T> void operator()(T i) const { /* … */ } template <typename T> auto To() const { // Doesn’t work! return this->*static_cast<void (Foo::*)(int) const>(&Foo::operator()); } }; Basically, I’d like to obtain a functor from a member function pointer. The above code would have ..

Read more

I have a callable struct Foo defined as struct Foo { template <typename T> void operator()(T i) const { /* … */ } }; and for reasons that are out of scope I would like to statically select which type to call it with avoiding the following cumbersome notation: Foo foo; foo.operator()<int>(0); foo.operator()<char>(‘0’); foo.operator()<char>(0); // ..

Read more

I have a variadic template functor class: template <typename Result, typename… Arguments> class Functor { public: using FunctionType = std::function<Result(Arguments…)>; Result operator() (const Arguments&… arguments) { return Function(arguments); } std::string GetName() { return Name; } Functor(const std::string& name, const FunctionType& function) : Name(name), Function(function) { } private: std::string Name; FunctionType Function; } and a variadic ..

Read more

I tried to make a device functor that essentially performs (unoptimized) matrix-vector multiplication like so namespace cusolve { template <class value_type, class matrix_type = value_type*, class vector_type = value_type*> struct linear_operator { const matrix_type matrix; const size_t width; __device__ linear_operator(const matrix_type matrix, size_t width) : matrix(matrix), width(width) { } __device__ void operator()(const vector_type x, vector_type ..

Read more

Given a functor which is supposed to move a value somewhere else, what is the correct way of capturing the "moved" value? template<typename TYPE> struct MovingFunctor { MovingFunctor(TYPE & moveto, TYPE ?? moved) : destination(moveto), capture(value){} void operator()() { moveto = std::move(capture); } TYPE & destination; TYPE ?? capture; }; int main() { std::unique_ptr<double> aValue; ..

Read more