5

Current code:

#include <utility>

template <typename T, void (*D)(T *)>
std::pair<T *, void (*)(T *)> f(T * const t)
{
    // ...
    return std::make_pair(t, D); // need `T` and `D` here
}

void d(int *t);

void g()
{
    auto something = f<int, d>(new int {5});        
}

What I'd like:

#include <utility>

template </* fill here: only `D` */>
std::pair<T *, void (*)(T *)> f(T * const t)
{
    // ...
    return std::make_pair(t, D); // need `T` and `D` here
}

void d(int *t);

void g()
{
    auto something = f<d>(new int {5});        
}

Is this possible in C++11?

eepp
  • 7,255
  • 1
  • 38
  • 56
  • What about [std::tuple](https://en.cppreference.com/w/cpp/utility/tuple)? Edit: after reading again, do you want to dynamically get a function's argument types? – DiantArts Apr 28 '22 at 03:58
  • No dynamically, at compile time. What I have in mind is something like `template `, and then you don't need to provide the second parameter because it's deduced from the type of the first parameter of `D`, but that doesn't work obviously. – eepp Apr 28 '22 at 04:08
  • @eepp What is `t` that you're passing as the function argument in `f(t)`? Can you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Jason Apr 28 '22 at 04:12
  • @Anya Edited: the first example builds and works. – eepp Apr 28 '22 at 04:19
  • @eepp Ok, now it looks much better. – Jason Apr 28 '22 at 04:19
  • This can be easily achieved using the C++17 Non-Type Template Argument, but there doesn't seem to be a relatively easy way for C++11. – 康桓瑋 Apr 28 '22 at 04:43

0 Answers0