1

in C/C++ (possibly pre-C++11), is it possible to do

A (*eval(A (*function)(B), B b))(){ 
  // ... ??
}

i.e., a function taking

  1. a function returning an A value from a B value,
  2. a B value to be fed to that function,

which returns - a function returning an A from () ...??

If yes, would it be

  • efficient??
  • guaranteed the compiler generates code which is not executed before call of the returned function??

Thanks in advance & cheers, Nick

2014-4-20 (1): Thanks for mentioning the 'evtl.'(fixed) std::bind. :-)

So – to understand – (in C/pre C++11 without Boost) function pointers are exceptional in the way that, inside functions, it is only possible to declare them, but there is no way to produce or modify an instance – as function/method definitions are the only possible sources for function pointer instances, from where these may be handed over either explicitly, or by function/method arguments??

Just asking, as I am not clear about a possible internal representation of function pointers...

2014-4-20 (2): With the contribution of Danvil, it's time for the purpose to reveal, here the same with templates:

template<typename T,typename A>
struct Evaluator {
  T(*f)(A);
  A a;
  T operator()() const { return f(a); }
};

template<typename T,typename A>
Evaluator<T,A> eval(T(*f)(A), A a) {
  Evaluator<T,A> w;
  w.f= f; w.a= a;
  return w;
}

This works, while – as some already might guess – the whole, from arbitrary matching function/arguments collections, is intended to be sent as a zero parameter procedure into a single function/method handling execution similar to a try/catch.

For not having to use mostly identical code for each different parameter count, the actual idea was to generate the still not executed job as a such zero parameter procedure of same type for all cases.

Still, I do not find a way how to construct or modify a function pointer inside a function; 'typecasting' in some way to Evaluator does not seem practicable, does it??

Again, thanks a lot, and Happy Easter... :-)

  • 2
    C/C++ doesn't exist. (And what does "evtl." mean?) – Alan Stokes Apr 19 '14 at 21:15
  • @AlanStokes evtl. means eventuell. – vallentin Apr 19 '14 at 21:17
  • ("Evtl." is short for the German word "eventuell", which in this context means "possibly" or "optionally".) – Martin R Apr 19 '14 at 21:18
  • C++03 - `boost::bind` (but it doesn't return function pointers, but functors). Without functors it's not possible to it robustly (evtl. you can use global variables - but that's not a proper solution) – milleniumbug Apr 19 '14 at 21:18
  • Ooops, Martin R got it - I am interfacing to a library, which (in the short run) might not support C++11, while Boost might be a real option,thanks a lot. – user1566867 Apr 20 '14 at 12:03

2 Answers2

5

I think you're looking for std::bind. The name std::bind is new, previously it was part of Boost.

#include <functional>

std::function<A (void)> curry(A (*fn)(B), B b)
{
    return std::bind(fn, b);
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Without C++11 it could work like this:

typedef A(*Func)(B);

struct Evaluator {
    Func f;
    B b;
    A operator()() const
    { return f(b); }
};

Evaluator eval(Func f, B b) {
    Evaluator w;
    w.f = f;
    w.b = b;
    return w;
}

That's essentially what std::bind is doing, so use std::bind if you can.

Danvil
  • 22,240
  • 19
  • 65
  • 88