This question already has answers here:
split variadic template arguments
(5 answers)
Closed 2 years ago.
I wrote a convenience function that gets some arguments and then sends them
to the right functions:
void combine(int a, int b, double one, double two) {
funA(a,b);
funB(one, two);
}
Now I want to do it again, but with variadic template parameters for both my combine function and the funA-funB pair:
//somewhere earlier in the code
template<class U, class T> funA(U par1, T par2) {...}
template<class X, class Y> funB(X par1, Y par2) {...}
template<class ...ParamA, class ...ParamB>
void combine(ParamA...packA, ParamB...packB) {
funA(packA...);
funB(packB...);
}
This of course can't work because I need some way to divide the parameters list in half.
But what is more interesting is when I try to compile the above code with a call like
combine(10, 'a', 12.0, true) I get this error:
In instantiation of ‘void combine(ParamA ..., ParamB ...) [with ParamA = {}; ParamB = {int, char, double, bool}]’:
...
error: no matching function for call to ‘funA()’
....
error: no matching function for call to ‘funB(int&, char&, double&, bool&)’
which shows that ParamB "ate" all of the parameter list.
So, my question is: is there a way to divide the parameter list of a function with variable template list in half?. If not, how else may I write my combine function?
#include <tuple>
#include <utility>
#include <iostream>
#include <memory>
//somewhere earlier in the code
template<class U, class T> void funA(U par1, T par2) {
std::cout<<par1 <<' '<< par2 <<'\n';
}
// Use move-only type to check forwarding
template<class X> void funB(X par1, std::unique_ptr<int> par2) {
std::cout<<par1 <<' '<< *par2 <<'\n';
}
// Call fnc with a subset of arguments, more general than we need for the partitioning.
template<typename Fnc,std::size_t...Is, typename...Args>
auto call_sub(Fnc fnc, std::index_sequence<Is...>, Args&&...args){
auto tup = std::forward_as_tuple(std::forward<Args>(args)...);
// Extract relevant indices.
return fnc(std::get<Is>(std::move(tup))...);
}
// Shift index sequences
template< std::size_t shift_amount,std::size_t...Is>
constexpr auto shift_sequence( std::index_sequence<Is...>){
return std::index_sequence<shift_amount +Is...>{};
}
template<class ...Params>
void combine(Params&&...pack) {
static_assert(sizeof...(pack) % 2 == 0);
constexpr std::size_t half = sizeof...(pack) /2;
constexpr std::make_index_sequence<half> first_half;
constexpr auto second_half = shift_sequence<half>(first_half);
// Lambda must be used because funA nor funB are not functions, they are templates which cannot be passed around.
call_sub([](auto&&...args){return funA(std::forward<decltype(args)>(args)...);},
first_half, std::forward<Params>(pack)...);
call_sub([](auto&&...args){return funB(std::forward<decltype(args)>(args)...);},
second_half, std::forward<Params>(pack)...);
}
int main (int argc, char *argv[])
{
combine(1,2,3,std::make_unique<int>(10));
}
Output:
1 2
3 10
Live Godbolt demo
Related
I'm trying to write a function for a template class which takes in a parameter that is a function pointer for a member class inside the private data of the big class. When you call that member, it calls that function on smaller class. (Confusing right?) To demonstrate, I have a non-working example here:
#include <vector>
#include <iostream>
using namespace std;
template <typename T, typename C>
struct MyClass {
template <typename F, typename... A>
auto call_me(F func, A... args) { // pass in the function we want to call
return (mContainer.*func) (args...); // call the function supplied by
// the parameter on the private member data
}
C mContainer; // this will be private in my actual code
};
int main() {
MyClass<int, std::vector<int> > test;;
cout << test.call_me(&std::vector<int>::size) << endl; // works
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4); // doesn't work
return 0;
}
Please note that this isn't my actual code but a small example of what I'm trying to do. As you can see, I'm trying to call the size member function of the 'Private' (I have kept it public here for demonstration) vector class inside MyClass. This only works whenever I have no parameters for the compiler to unpack, but when I try to do the insert function (which has parameters to unpack), the compiler gives me an error of:
.\template.cpp: In function 'int main()':
.\template.cpp:24:71: error: no matching function for call to 'MyClass<int, std::vector<int> >::call_me(<unresolved overloaded function type>, std::vector<int>::iterator, int)'
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4);
^
.\template.cpp:10:10: note: candidate: template<class F, class ... A> auto MyClass<T, C>::call_me(F, A ...) [with F = F; A = {A ...}; T = int; C = std::vector<int>]
auto call_me(F func, A... args) { // pass in the function we want to call
^~~~~~~
.\template.cpp:10:10: note: template argument deduction/substitution failed:
.\template.cpp:24:71: note: couldn't deduce template parameter 'F'
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4);
This is the same error I'm getting in my actual production code, calling the variadic function with no parameters to unpack works, but if I give more than that, I get the same error message. This is my first real attempt to use Variadic templates, so any recommendation and help will be appreciated.
The problem here is that insert is an overloaded function. The compiler is not doing to try and resolve what overload you want in template argument deduction as there is no way for it to know. You have to cast the function to the type of the overload you want to use in order to give it a type. That would look like
using insert_func_t = std::vector<int>::iterator(std::vector<int>::*)(std::vector<int>::const_iterator, const int&);
test.call_me(static_cast<insert_func_t>(&std::vector<int>::insert), test.mContainer.begin(), 4);
In general it is
static_cast<return_type(class_name::*)(function_parameters)>(&class_name::function_name)
Another option would be to change the function a little and take a lambda that expresses what you want done. That would look like
template <typename T, typename C>
struct MyClass {
template <typename F, typename... A>
auto call_me(F func, A... args) { // pass in the function we want to call
return func(mContainer, args...); // call the function supplied by
// the parameter on the private member data
}
C mContainer; // this will be private in my actual code
};
int main() {
MyClass<int, std::vector<int> > test;;
test.call_me([](auto& container, auto... args){ container.insert(args...); }, test.mContainer.begin(), 4);
return 0;
}
Basically you cannot take address of an unresolved overloaded function, because the compiler won't be able to choose the right function entry point address. During normal function call the compiler resolves overloaded function, but with templates like yours or std::bind() this won't work, because the parameters are used to call the template function, not the function you want to take address of.
You can manually resolve the overload like this:
using ftype = std::vector<int>::iterator(std::vector<int>::*)
(std::vector<int>::const_iterator, const std::vector<int>::value_type&);
test.call_me((ftype)(&std::vector<int>::insert), test.mContainer.begin(), 4); // works
It's easier to deal in function objects when doing this kind of thing. It offloads the problem of method overloads to the compiler.
Lambdas also work (they're function objects):
#include <vector>
#include <iostream>
template <typename T, typename C>
struct MyClass {
template <typename F, typename... A>
auto call_me(F func, A&&... args) -> decltype(auto)
{ // pass in the function we want to call
return func(mContainer, std::forward<A>(args)...); // call the function supplied by
// the parameter on the private member data
}
C mContainer; // this will be private in my actual code
};
/*
* It's often easier to deal in function objects
*/
struct insert
{
template<class Container, class...Args>
decltype(auto) operator()(Container& cont, Args&&...args) const
{
return cont.insert(std::forward<Args>(args)...);
}
};
struct size
{
template<class Container, class...Args>
decltype(auto) operator()(Container& cont) const
{
return cont.size();
}
};
int main() {
MyClass<int, std::vector<int> > test;;
std::cout << test.call_me(size()) << std::endl; // works
test.call_me(insert(), test.mContainer.begin(), 4); // doesn't work
// or lambdas
auto insert2 = [](auto& container, auto&&...args) -> decltype(auto)
{
return container.insert(std::forward<decltype(args)>(args)...);
};
test.call_me(insert2, test.mContainer.begin(), 5);
return 0;
}
I try to pass to a variadic template function a list of references and pass it to another function. The code that I wrote is the following:
template <typename T>
void fun(cv::Point_<T> & pt) { pt.x++; pt.y++; }
template <class ... args>
void caller(args & ... list) {
typedef typename std::tuple_element<0, std::tuple<args...> >::type T;
std::array<std::reference_wrapper<T>, sizeof...(list)> values {list ... };
for(int i=0; i<values.size(); i++)
fun(values[i]);
}
then I call the function caller in this way:
cv::Point2f a, b, c;
caller(a, b, c);
the compiler give me the following error:
No matching function for call to 'fun'
Candidate template ignored: could not match 'Point_' against 'reference_wrapper'
what I missing?
Although std::reference_wrapper<T> has an implicit conversion to T&, you cannot use both an implicit conversion and template argument deduction at the same time, and template argument deduction is necessary to call fun.
Try
fun(values[i].get());
Even simpler is
template <typename...Args>
void caller(Args&...args)
{
auto tmp = { (func(args),0)..., 0 };
}
This uses the fact that parameter pack expansion can occur in braced init lists. Since func() returns void, we cannot simply use { func(args)... }, but use (func(args),0) to have an int. Finally, the last 0 is to ensure that the code compiles (and does nothing) in case of an empty parameter pack.
You can generalise this and write a template that calls a given generic function for every element of a pack:
template <typename Func, typename...Args>
void call_for_each(Func &&func, Args&&...args)
{
auto unused = { (func(std::forward<Args>(args)),0)...,0 };
}
which may be used like this (C++14)
int main()
{
int a=1;
double b=2.4;
auto func = [](auto&x) { std::cout<<' '<<x++; };
call_for_each(func,a,b);
std::cout<<'\n';
call_for_each(func,a,b);
std::cout<<'\n';
}
This uses a C++14 lambda (taking an auto argument). Note that the parameter pack must come last among the template parameters of call_for_each.
Since the goal of this might be to iterate over all args, here's a more generic solution. We are going to implement for_pack:
template<typename... Args, typename F>
void for_pack(F function, Args&&... args) {
using expand = int[];
(void)expand{(function(std::forward<Args>(args)), void(), 0)..., 0};
}
This will execute function for every args in Args.
Now, your function caller is much more trivial to implement:
template <typename... args>
void caller(args&... list) {
for_pack([&](cv::Point_<T>& arg){
fun(arg);
}, list...);
}
Since a google search for "c++ pass reference parameters to variadic template" gives this as first result, I'll put this generic solution here.
struct HH { /*...*/ void change_me() { /*...*/ } };
template<typename...T> void parms_r_refs() {}
template<typename H, typename...T> void parms_r_refs(H &h, T&...t) { h.change_me(); parms_r_refs(t...); }
template<typename...T> void parms_r_refs(T&...t) { parms_r_refs(t...); }
HH a, b, c;
..
parms_r_refs(a, b, c);
..
I am getting an error when using template type deduction combined with C++14 std::get<> with type indices. The code might look a little complex, but I've tried to whittle it down to the bare basics of what's going on. It's really just an Observer pattern... The struct 'A' allows observers to be set based on message type (M1, M2, ...). Note that there is only one observer per message type, in order to keep things simple.
Now the trick (and the part that is failing) is using C++14's std::get<>, which allows you to index into a tuple of unique types using the actual type. Here is a simple example demonstrating what I mean:
void sample()
{
std::tuple<int, float> myTuple;
std::get<float>(myTuple) = 3.141f; // C++14 allows this
std::get<1>(myTuple) = 3.141f; // C++11 way to do it, using index
}
With this in mind, here is my program (separate from the above code) that does NOT compile because the C++14 tuple type indexing is failing on an inferred type:
#include <cxxabi.h>
#include <stdlib.h>
#include <functional>
#include <vector>
#include <tuple>
#include <typeinfo>
#include <iostream>
#include <string>
// ===================================
// A quick'n'dirty way to print types (nonportable)
// And yes, I know this code could be improved :)
inline
std::string demangle(char const *mangled)
{
char *output = (char *)malloc(16384);
size_t length = 16384;
int status;
__cxxabiv1::__cxa_demangle(mangled, output, &length, &status);
std::string s(output, length);
free(output);
return s;
}
#define DEMANGLE(T) demangle(typeid(T).name())
// ===================================
struct A
{
struct M1
{};
struct M2
{};
using Tuple = std::tuple<
std::function<void(M1 const &)>
,std::function<void(M2 const &)>
>;
template<typename T>
void setObserver(T func)
{
// This works fine
std::cout << DEMANGLE(T) << std::endl;
// ************************************************
// The line below does not compile (std::get fails)
//
// Note the type of T prints out as:
// std::_Bind<std::_Mem_fn<void (B::*)(A::M1 const&)> (B*, std::_Placeholder<1>)>
//
// Rather than the (desired):
// std::function<void (A::M1 const&)>(A::M1 const&)> (B*, std::_Placeholder<1>)>
//
// ************************************************
std::get<T>(tuple_) = func; // C++14 only
}
private:
Tuple tuple_;
};
// ===================================
struct B
{
void func(A::M1 const &)
{}
};
// ===================================
int main()
{
A *a = new A;
B *b = new B;
using namespace std::placeholders;
a->addObserver(std::bind(&B::func, b, _1));
return 0;
}
UPDATE:
The proposed solution does solve the problem of converting from std::bind(...) to std::function(...), BUT it requires me to have a separate setObserver() function for each of my types M1, M2, ...
How can I templatize setObserver() to fix this?
std::bind()does not return std::functionbut some unspecified type convertible to std::function<>. Secondly, std::get<>() in C++14 form requires exact type, not just anything convertible to one of tuples member types.
To achieve what you want - you need to convert your T to one of your tuple types.
E.g. you might move your current setObserver to private section and rename it - and create functions for your desired types:
template<typename T>
auto setObserver(T func)
-> typename std::enable_if<std::is_convertible<T, std::function<void(M1 const &)>>::value>::type
{
this->template setObserverImpl<std::function<void(M1 const &)>>(func);
}
template<typename T>
auto setObserver(T func)
-> typename std::enable_if<std::is_convertible<T, std::function<void(M2 const &)>>::value>::type
{
this->template setObserverImpl<std::function<void(M2 const &)>>(func);
}
private:
template<typename T>
void setObserverImpl(T func)
{
// no change here
}
You need to make the current setObserver to be template function unless you have just two types.
To template this solution - either use variant functions for each Mx type where "non convertible version" will be empty:
template <typename T, typename M>
auto setObserverTemplate(T func) -> typename std::enable_if<std::is_convertible<T, std::function<void(M const &)>>::value>::type
{
this->template setObserverImpl<std::function<void(M const &)>>(func);
}
template <typename T, typename M>
auto setObserverTemplate(T) -> typename std::enable_if<not std::is_convertible<T, std::function<void(M const &)>>::value>::type
{
// do nothing
}
template<typename T>
auto setObserver(T func)
{
this->template setObserverTemplate<T,M1>(func);
this->template setObserverTemplate<T,M2>(func);
}
Or, probably much better - variadic version with sentinel empty function:
template<typename T, typename MFirst, typename ...M>
auto setObserverVariadic(T func)
{
this->template setObserverTemplate<T,MFirst>(func);
this->template setObserverVariadic<T,M...>(func);
}
template<typename T>
auto setObserverVariadic(T)
{
}
template<typename T>
auto setObserver(T func)
{
this->template setObserverVariadic<T,M1,M2>(func);
}
Last comment - you might try to "retrieve" current types (I mean these list of Mx types) from your tuple type. How to do it - that could be good candidate for new question.
Given a function f(x, y, z) we can bind x to 0, getting a function g(y, z) == f(0, y, z). We can continue doing this and get h() = f(0, 1, 2).
In C++ syntax that would be
#include <functional>
#include <iostream>
void foo(int a, long b, short c)
{
std::cout << a << b << c << std::endl;
}
int main()
{
std::function<void(int, long, short)> bar1 = foo;
std::function<void(long, short)> bar2 = std::bind(bar1, 0, std::placeholders::_1, std::placeholders::_2);
std::function<void(short)> bar3 = std::bind(bar2, 1, std::placeholders::_1);
std::function<void()> bar4 = std::bind(bar3, 2);
bar4(); // prints "012"
return 0;
}
So far so good.
Now say that I want to do the same -- bind the first argument of a function, get the new function back and repeat this process until all arguments are binded -- but generalize it to work not only with a function of 3 arguments as in the C++ example above, but with a function with unknown* number of arguments.
* In C++ there is such thing as variadic arguments and in C++11 there are variadic templates. I'm referring to variadic templates here.
Basically, what I want to be able to do, is to write a function that accepts any std::function and recursively binds the first argument to some value until all arguments are binded and the function can be called.
For the simplicity, let's assume that std::function represents a function taking any integral arguments and returning void.
This code can be considerate to be a generalization of the previous code
#include <functional>
#include <iostream>
// terminating case of recursion
void apply(std::function<void()> fun, int i)
{
fun();
}
template<class Head, class... Tail>
void apply(std::function<void(Head, Tail...)> f, int i)
{
std::function<void(Tail...)> g = std::bind(f, i);
apply<Tail...>(g, ++i);
}
void foo(int a, long b, short c)
{
std::cout << a << b << c << std::endl;
}
int main()
{
std::function<void(int, long, short)> bar1 = foo;
apply<int, long, short>(bar1, 0);
return 0;
}
This code is great. It is exactly what I want. It doesn't compile.
main.cpp: In instantiation of 'void apply(std::function<void(Head, Tail ...)>, int) [with Head = int; Tail = {long int, short int}]':
main.cpp:24:40: required from here
main.cpp:12:56: error: conversion from 'std::_Bind_helper<false, std::function<void(int, long int, short int)>&, int&>::type {aka std::_Bind<std::function<void(int, long int, short int)>(int)>}' to non-scalar type 'std::function<void(long int, short int)>' requested
std::function<void(Tail...)> g = std::bind(f, i);
^
The issue is that you can't just leave out std::placeholders in std::bind call like that. They are required, and number of placeholders in std::bind should match the number of non-binded arguments in the function.
If we change line
std::function<void(Tail...)> g = std::bind(f, i);
to
std::function<void(Tail...)> g = std::bind(f, i, std::placeholders::_1, std::placeholders::_2);
we see that it successfully passes through the first apply() call, but gets stuck on the second pass, because during the second pass g needs only one placeholder, while we still have two of them in the std::bind.
main.cpp: In instantiation of 'void apply(std::function<void(Head, Tail ...)>, int) [with Head = long int; Tail = {short int}]':
main.cpp:13:30: required from 'void apply(std::function<void(Head, Tail ...)>, int) [with Head = int; Tail = {long int, short int}]'
main.cpp:24:40: required from here
main.cpp:12:102: error: conversion from 'std::_Bind_helper<false, std::function<void(long int, short int)>&, int&, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind<std::function<void(long int, short int)>(int, std::_Placeholder<1>, std::_Placeholder<2>)>}' to non-scalar type 'std::function<void(short int)>' requested
std::function<void(Tail...)> g = std::bind(f, i, std::placeholders::_1, std::placeholders::_2);
^
There is a way to solve that using regular non-variadic templates, but it introduces a limit on how many arguments std::function can have. For example, this code works only if std::function has 3 or less arguments
(replace apply functions in the previous code on these)
// terminating case
void apply(std::function<void()> fun, int i)
{
fun();
}
template<class T0>
void apply(std::function<void(T0)> f, int i)
{
std::function<void()> g = std::bind(f, i);
apply(g, ++i);
}
template<class T0, class T1>
void apply(std::function<void(T0, T1)> f, int i)
{
std::function<void(T1)> g = std::bind(f, i, std::placeholders::_1);
apply<T1>(g, ++i);
}
template<class T0, class T1, class T2>
void apply(std::function<void(T0, T1, T2)> f, int i)
{
std::function<void(T1, T2)> g = std::bind(f, i, std::placeholders::_1, std::placeholders::_2);
apply<T1, T2>(g, ++i);
}
But the issue with that code is that I would have to define a new apply function to support std::function with 4 arguments, then the same with 5 arguments, 6 and so on. Not to mention that my goal was to not have any hard-coded limit on the number of arguments. So this is not acceptable. I don't want it to have a limit.
I need to find a way to make the variadic template code (the second code snippet) to work.
If only std::bind didn't require to specify placeholders -- everything would work, but as std::bind currently works, we need to find some way to specify the right number of placeholders.
It might be useful to know that we can find the right number of placeholders to specify with C++11's sizeof...
sizeof...(Tail)
but I couldn't get anything worthwhile out of this fact.
First, stop using bind unless you absolutely need to.
// terminating case of recursion
void apply(std::function<void()> fun, int i) {
fun();
}
// recursive case:
template<class Head, class... Tail>
void apply(std::function<void(Head, Tail...)> f, int i) {
// create a one-shot lambda that binds the first argument to `i`:
auto g = [&](Tail&&...tail) // by universal ref trick, bit fancy
{ return std::move(f)(std::move(i), std::forward<Tail>(tail)...);};
// recurse:
apply<Tail...>(g, ++i);
}
next, only type erase if you have to:
// `std::resukt_of` has a design flaw. `invoke` fixes it:
template<class Sig,class=void>struct invoke{};
template<class Sig>using invoke_t=typename invoke<Sig>::type;
// converts any type to void. Useful for sfinae, and may be in C++17:
template<class>struct voider{using type=void;};
template<class T>using void_t=typename voider<T>::type;
// implementation of invoke, returns type of calling instance of F
// with Args...
template<class F,class...Args>
struct invoke<F(Args...),
void_t<decltype(std::declval<F>()(std::declval<Args>()...))>
>{
using type=decltype(std::declval<F>()(std::declval<Args>()...));
};
// tells you if F(Args...) is a valid expression:
template<class Sig,class=void>struct can_invoke:std::false_type{};
template<class Sig>
struct can_invoke<Sig,void_t<invoke_t<Sig>>>
:std::true_type{};
now we have some machinery, a base case:
// if f() is a valid expression, terminate:
template<class F, class T, class I,
class=std::enable_if_t<can_invoke<F()>{}>
>
auto apply(F&& f, T&& t, I&&i)->invoke_t<F()>
{
return std::forward<F>(f)();
}
which says "if we can be invoked, just invoke f.
Next, the recursive case. It relies on C++14 return type deduction:
// if not, build lambda that binds first arg to t, then recurses
// with i(t):
template<class F, class T, class I,
class=std::enable_if_t<!can_invoke<F()>{}, int>>
>
auto apply(F&& f, T&& t, I&&i)
{
// variardic auto lambda, C++14 feature, with sfinae support
// only valid to call once, which is fine, and cannot leave local
// scope:
auto g=[&](auto&&...ts) // takes any number of params
-> invoke_t< F( T, decltype(ts)... ) > // sfinae
{
return std::forward<F>(f)(std::forward<T>(t), decltype(ts)(ts)...);
};
// recurse:
return apply(std::move(g), i(t), std::forward<I>(i));
}
If you want increment, pass [](auto&&x){return x+1;} as 3rd arg.
If you want no change, pass [](auto&&x){return x;} as 3rd arg.
None of this code has been compiled, so there may be typos. I am also worried about the recursion of apply with C++14 return type deduction, that gets tricky sometimes.
If you really have to use bind, you can define your own placeholder types by specializing std::is_placeholder:
template<int N>
struct my_placeholder { static my_placeholder ph; };
template<int N>
my_placeholder<N> my_placeholder<N>::ph;
namespace std {
template<int N>
struct is_placeholder<::my_placeholder<N>> : std::integral_constant<int, N> { };
}
The reason this is useful is that it allows you to then map an integer to a placeholder at compile time, which you can use with the integer_sequence trick:
void apply(std::function<void()> fun, int i)
{
fun();
}
template<class T, class... Ts>
void apply(std::function<void(T, Ts...)> f, int i);
template<class T, class... Ts, int... Is>
void apply(std::function<void(T, Ts...)> f, int i, std::integer_sequence<int, Is...>)
{
std::function<void(Ts...)> g = std::bind(f, i, my_placeholder<Is + 1>::ph...);
apply(g, ++i);
}
template<class T, class... Ts>
void apply(std::function<void(T, Ts...)> f, int i) {
apply(f, i, std::make_integer_sequence<int, sizeof...(Ts)>());
}
Demo. make_integer_sequence and friends are C++14, but can be implemented easily in C++11.
If you're prepared to drop std::bind (which really was a bit of a hacky workaround for pre-C++11 partial applications in my view) this can be quite concisely written:
#include <functional>
#include <iostream>
// End recursion if no more arguments
void apply(std::function<void()> f, int) {
f();
}
template <typename Head, typename ...Tail>
void apply(std::function<void(Head, Tail...)> f, int i=0) {
auto g = [=](Tail&& ...args){
f(i, std::forward<Tail>(args)...);
};
apply(std::function<void(Tail...)>{g}, ++i);
}
void foo(int a, int b, int c, int d) {
std::cout << a << b << c << d << "\n";
}
int main() {
auto f = std::function<void(int,int,int,int)>(foo);
apply(f);
}
Tested working with clang 3.4 and g++ 4.8.2 in C++11 mode. Also on ideone.
You don't need to use std::bind recursively to call some function with a tuple of parameters which values can be evaluated using parameter index:
#include <functional>
#include <utility>
template <typename... Types, std::size_t... indexes, typename Functor>
void apply(std::function<void(Types...)> f, std::index_sequence<indexes...>, Functor&& functor)
{
f(static_cast<Types>(std::forward<Functor>(functor)(indexes))...);
}
template <typename... Types, typename Functor>
void apply(std::function<void(Types...)> f, Functor&& functor)
{
apply(f, std::make_index_sequence<sizeof...(Types)>{}, std::forward<Functor>(functor));
}
Example of use:
void foo(int a, long b, short c)
{
std::cout << a << b << c << std::endl;
}
// ...
std::function<void(int, long, short)> bar = foo;
apply(bar, [](std::size_t index){ return (int)index; });
Live demo
As #T.C. noted in his answer std::make_index_sequence is a C++14 feature but it can be implemented in C++11.
Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) :
#include <iostream>
#include <type_traits>
#include <ratio>
template<int Int, typename Type>
constexpr Type f(const Type x)
{
return Int*x;
}
template<class Ratio, typename Type,
class = typename std::enable_if<Ratio::den != 0>::type>
constexpr Type f(const Type x)
{
return (x*Ratio::num)/Ratio::den;
}
template</*An int OR a type*/ Something, typename Type>
constexpr Type g(const Type x)
{
return f<Something, Type>(x);
}
int main()
{
std::cout<<f<1>(42.)<<std::endl;
std::cout<<f<std::kilo>(42.)<<std::endl;
}
As you can see, there are two versions of the f() function : the first one takes an int as a template parameter, and the second one takes a std::ratio. The problem is the following :
I would like to "wrap" this function through g() which can take an int OR a std::ratio as first template parameter and call the good version of f().
How to do that without writing two g() functions ? In other words, what do I have to write instead of /*An int OR a type*/ ?
Here's how I would do it, but I've changed your interface slightly:
#include <iostream>
#include <type_traits>
#include <ratio>
template <typename Type>
constexpr
Type
f(int Int, Type x)
{
return Int*x;
}
template <std::intmax_t N, std::intmax_t D, typename Type>
constexpr
Type
f(std::ratio<N, D> r, Type x)
{
// Note use of r.num and r.den instead of N and D leads to
// less probability of overflow. For example if N == 8
// and D == 12, then r.num == 2 and r.den == 3 because
// ratio reduces the fraction to lowest terms.
return x*r.num/r.den;
}
template <class T, class U>
constexpr
typename std::remove_reference<U>::type
g(T&& t, U&& u)
{
return f(static_cast<T&&>(t), static_cast<U&&>(u));
}
int main()
{
constexpr auto h = g(1, 42.);
constexpr auto i = g(std::kilo(), 42.);
std::cout<< h << std::endl;
std::cout<< i << std::endl;
}
42
42000
Notes:
I've taken advantage of constexpr to not pass compile-time constants via template parameters (that's what constexpr is for).
g is now just a perfect forwarder. However I was unable to use std::forward because it isn't marked up with constexpr (arguably a defect in C++11). So I dropped down to use static_cast<T&&> instead. Perfect forwarding is a little bit overkill here. But it is a good idiom to be thoroughly familiar with.
How to do that without writing two g() functions ?
You don't. There is no way in C++ to take either a type or a value of some type, except through overloading.
It is not possible to have a template parameter taking both type and non-type values.
Solution 1:
Overloaded functions.
Solution 2:
You can store values in types. Ex:
template<int n>
struct store_int
{
static const int num = n;
static const int den = 1;
};
template<class Ratio, typename Type,
class = typename std::enable_if<Ratio::den != 0>::type>
constexpr Type f(const Type x)
{
return (x*Ratio::num)/Ratio::den;
}
template<typename Something, typename Type>
constexpr Type g(const Type x)
{
return f<Something, Type>(x);
}
But with this solution you will have to specify g<store_int<42> >(...) instead of g<42>(...)
If the function is small, I advise you to use overloading.