last day (14 days later) » 

02:59
3
A: Varying the Parameter List of an Function Based on Template Parameter?

Kerrek SBThis shouldn't be too hard. Let's start with the top level: template <typename C, std::size_t N> struct NaryDispatch { // details, see below using f_type = typename function_maker<C &, N>::type; template <typename F> NaryDispatch(F && f) : fn_(std::forward<F>(f)) {} f_type...

Change Args... to Args&... since he wants references.
I couldn't get C& to work for some reason. Replacing the second T parameter to function_maker to T& in the primary template worked, as did changing Args... to Args&... in the specialization.
Probably just a small mistake on my part, I see it working in your demo.
Absolutely perfect! Works exactly as intended and a very elegant solution to boot. Unfortunately, in my shortsightedness, I forgot to consider how exactly I might invoke this function if it has an arbitrary number of parameters. The goal would be to supply the parameters by selecting N elements from a container at run-time. I immediately went looking for some form of "currying" support but found nothing. Oh well, perhaps an issue for another question. Thanks again!
@DaveStance What was the N template parameter for if you wanted to know the arguments at runtime?
@0x499602D2, it specifies the arity of the function. The user should be able to specify a function taking N arguments of the specified type, at runtime the intention was to take N elements of an std::vector and invoke the function with these elements as the arguments.
@DaveStance Correct me if I'm wrong, but the user can specify the number of parameters with N, and then calling the function would be as simple as passing in the elements of the vector func(v[0], v[1], ...), correct?
02:59
@0x499602D2, indeed that's what I'm going for. The issue is the "..." however. Is there a way to expand an arbitrary selection of a vector elements based on a template parameter?
@DaveStance I don't think that's possible since the size of the vector isn't known at compile time, there's no way you can unpack a parameter pack v[I]... without knowing the size of v beforehand.
@DaveStance Maybe you can use iterators instead and have your fn_ function be function template that takes two template arguments as iterators. Then you can pass in your range without writing out every argument.
@0x499602D2 Ha! That's spot on what my original implementation looked like before I tried messing about with all of this TMP.
How did that work out for you?
Fairly well, it wasn't totally transparent to the user how it was working (i.e. that std::distance(itr1, itr2) == N) but I guess that's what documentation is for. It also required that they dereference the iterators and do their own iteration logic to access specific elements. I mean not the worst thing ever, but [](int a, int b, int c){} would have been so much cleaner.
Hmm... can you consider using std::array or a container with a compile time size?
03:04
@0x499602D2 We're on the same wavelength. Tried that too, but the content type is a reference.
That's okay, just use a std::reference_wrapper -- en.cppreference.com/w/cpp/utility/functional/reference_wrapper
@0x499602D2, yeah tried. Can't initialize an std::array<std::reference_wrapper<T>, N> from a vector.
Can I see that code where you do that?
Where I try to use the reference wrapper?
Yes, and the code that causes the error.
03:08
The error is caused by trying to default-construct a std::array of std::reference_wrappers because it tries to default initialize N reference_wrappers but they cannot be default initialized (obviously nothing to reference).
If you know of a way to initialize say an std::array<std::reference_wrapper<int>, 5> from a std::vector<int> that solution would work just fine for me.
I believe there's a way. I'm working on it right now. please hold
@0x499602D2 Excellent! Thank you so much for this!
03:22
Is this good? The size still has to be known at compile time -- rextester.com/RHE41527
Damn. Nice implementation though the issue is that the goal is to provide the user's function with an N-length subsection of a larger, hidden vector. No biggy though, I'm beginning to think for all the difficulty I'm going to end up with a performance hit and nothing to show for it but a slightly sexier function signature.
Implementation is excellent though, thank you for the example! Still looking it through to see what you did.
03:38
No problem! :)

last day (14 days later) »