2

I am looking for a way to pass a generic (constexpr, obviously) function to a template. It has to be able to take any amount of parameters, without using a lambda. This is what I have so far:

template<typename T, T(*FUNC)()> struct CALL
{
    static inline constexpr decltype(FUNC()) EXEC()
    {
        return FUNC();
    }
};

This however only works if the passed function takes no parameters. Is there a way to make the template accept ANY constexpr function? Passing a std::function does not seem to work. I suppose the key is variadic template parameters, but I have no idea how to take advantage of them in this situation.

11
  • 1
    Why the constexpr hangup? Why would you think it's relevant/necessary?
    – ildjarn
    Jan 12, 2013 at 21:11
  • You can't possibly allow callers to pass in any function and expect your code to do anything useful with it. There has to be some restrictions in place. Are you prepared to call functions that requires 20 arguments? How about functions that require a platform-specific data type? What you're asking creates more problems than it solves.
    – In silico
    Jan 12, 2013 at 21:11
  • @In silico Why not? All I would have to do is to pass the parameters to the template as well. I just don't know how. @ ildjarn What do you mean by "hangup"?
    – Byzantian
    Jan 12, 2013 at 21:12
  • @cyberpunk_: ildjarn is asking why constexpr is relevant to your question.
    – In silico
    Jan 12, 2013 at 21:14
  • 3
    @cyberpunk_: Do you want thefunction to be called at compile-time? You're passing the function pointer as a non-type template parameter, so the pointer is already available at compile time, and hence constexpr is not required. It will still be called at runtime. However, what scenario do you think this will be useful for? I have a feeling that you're asking about a possible solution and not your actual problem.
    – In silico
    Jan 12, 2013 at 21:18

2 Answers 2

7

If I understand correctly what you are trying to achieve, you can use a template function rather than a template class with a static function:

#include <iostream>

template<typename T, typename... Ts>
constexpr auto CALL(T (*FUNC)(Ts...), Ts&&... args) -> decltype(FUNC(args...))
{
    return FUNC(std::forward<Ts>(args)...);
}

constexpr double sum(double x, double y)
{
    return (x + y);
}

int main()
{
    constexpr double result = CALL(sum, 3.0, 4.0);
    static_assert((result == 7.0), "Error!");
    return 0;
}
11
  • Thanks, but do I really have to use a constepxr function? The whole idea behind this is to force a constexpr function to be evaluated during compile-time. The reason why I was using a template class, is because templates MUST be evaluated at compile time, while constexpr function are also runtime callable. So, is there a way to implement your solution only using a template class?
    – Byzantian
    Jan 12, 2013 at 22:28
  • 1
    I think you are making some confusion when you say that templates must be "evaluated" at compile-time. templates are "instantiated" at compile-time, which is something different. no code gets executed during instantiation, only generated
    – Andy Prowl
    Jan 12, 2013 at 22:49
  • @cyberpunk_: so yes you have to use a constexpr function if you want some compile-time code execution (= evaluation) to be performed.
    – Andy Prowl
    Jan 12, 2013 at 22:51
  • @AndyProwl I know what he's trying to achieve, it's not that, he want template non-type parameters being passed as arguments to a function which the return goes to a constexpr before getting it, all to enforce compile-time code execution (=evaluation) of a constexpr function in a shorter form than by initializing a constexpr variable with the return of the function. He is chasing his own tail on that =D stackoverflow.com/q/14294271/1000282
    – oblitum
    Jan 13, 2013 at 16:33
  • @chico: I trust you, but I don't understand your explanation either :) anyway, my point is that during template instantiation there's no code being executed, not even code of a constexpr function. template meta-programming does allow to do some computation in the form (rather, in the process) of code generation, but that's like another language, more similar to functional programming than to regular procedural programming. and in any case it won't execute the body of any function passed in as a template argument, not even constexpr ones
    – Andy Prowl
    Jan 13, 2013 at 16:36
0
template<int... I>
struct with
{
    template<int F(decltype(I)...)>
    struct call
    {
        static constexpr int value = F(I...);
    };
};

constexpr int f(int i) {return i;}
constexpr int g(int i, int j) {return i + j;}

int main()
{
    int u = with<0>::call<f>::value;
    constexpr int v = with<0, 1>::call<g>::value;
}

Note, this has some of the limitations as in your previous question which I answer with std::integral_constant. But a constexpr double value could still be generated from non-type template arguments at compilation time.

#include <iostream>

template<typename T, T... v>
struct with
{
    template<typename R, R f(decltype(v)...)>
    struct call
    {
        static constexpr R value = f(v...);
    };
};

#define AT_COMPILATION(f, x, ...) with<decltype(x), x, ##__VA_ARGS__>::call<decltype(f(x, ##__VA_ARGS__)), f>::value

constexpr long f(long i) {return i;}
constexpr double g(int i, int j) {return static_cast<double>(i) / j;}

int main()
{
    constexpr double u = with<long, 0L>::call<decltype(f(0L)), f>::value;

    std::cout << with<int, 5, 2>::call<double, g>::value << std::endl;

    constexpr double v = AT_COMPILATION(f, 0L);

    std::cout << AT_COMPILATION(g, 5, 2) << std::endl;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.