0

I have a templated function wrapper with template type equal to return type of the function. Further the function's first argument is a pointer to the wrapper itself.

template<typename R>
struct wrapper_t{
    using result_t = R;
    template<typename F, typename ...Ts>
    wrapper_t(F &&, Ts &&...){

    }
};
int foo(wrapper_t<int>*,double){
    return 2;
}

I'm trying to use user-defined deduction guides here in order to avoid specifying the template type such that I can use it like

wrapper_t wrapper(foo,1.0);

But I don't know with what I should replace the question marks in the following guide with, because of the recursion.

template<typename F, typename ...Ts>
wrapper_t(F &&, Ts &&...)->wrapper_t<std::invoke_result_t<F,/*???*/,Ts...>>;

Is ctad still doable here, if so how?

ezegoing
  • 526
  • 1
  • 4
  • 18
  • Well, I could easily write a `bar` that takes arbitrary `wrapper_t` and returns `T`. What would be the correct deduction for `wrapper_t wrapper(bar);`? Basically, `F` defines a transformation on types, and you want to find a unique fixed point of that transformation. I suspect this is equivalent to the halting problem. – Igor Tandetnik Nov 18 '19 at 03:01
  • I see, though the wrapper's type is the function's return type. So the function's return type must be known. By now I have come up with `templatewrapper_t(F &&f, Ts &&...args)->wrapper_t(f)(uintptr_t(),std::forward(args)...))>;`. It works like a charm, still not 100% sure if such implicit casting of `uintptr_t` is of any trouble, but I think it is ok. – ezegoing Nov 18 '19 at 04:04
  • 1
    You are just passing `NULL`. I suspect `nullptr_t` would work just as well. `uintptr_t()` is a fancy way to write a null pointer constant. Note though that this will stop working as soon as `F` is overloaded, or is a template, or otherwise accepting more than one possible `wrapper_t` specialization. – Igor Tandetnik Nov 18 '19 at 04:48
  • @ezegoing, what is your use case where the function needs the wrapper to be invoked? Since the invocation has the function and the arguments, which are also the constructor arguments to the wrapper, it seems redundant. – Jeff Garrett Nov 21 '19 at 17:19
  • @JeffGarrett I can think of many, this is the one I stumbled upon https://probablydance.com/2012/11/18/implementing-coroutines-with-ucontext/. – ezegoing Nov 21 '19 at 18:00

0 Answers0