Consider this code:
#include <iostream>
#include <type_traits>
template <std::size_t N> void bar() { std::cout << "bar<" << N << ">() called.\n"; }
template <std::size_t N> void hit() { std::cout << "hit<" << N << ">() called.\n"; }
template <typename T> struct evaluate : std::bool_constant<std::is_integral_v<T>> {
static constexpr std::size_t size = sizeof(T); // Simplified for illustration only.
};
void foo() { }
template <typename T, typename... Args>
std::enable_if_t<!evaluate<T>::value> foo (const T&, const Args&...);
template <typename T, typename... Args>
std::enable_if_t<evaluate<T>::value> foo (const T&, const Args&... args) {
bar<evaluate<T>::size>();
// Do whatever.
foo(args...);
}
template <typename T, typename... Args>
std::enable_if_t<!evaluate<T>::value> foo (const T&, const Args&... args) {
hit<evaluate<T>::size>();
// Do whatever, but different from the previous foo overload.
foo(args...);
}
int main() {
foo (5, "hello", true);
}
Output:
bar<4>() called.
hit<6>() called.
bar<1>() called.
How to rewrite the above so that evaluate<T> needs only be computed once instead of twice with each foo iteration?
You maybe like this one:
template <std::size_t N> void bar() { std::cout << "bar<" << N << ">() called.\n"; }
template <std::size_t N> void hit() { std::cout << "hit<" << N << ">() called.\n"; }
template <typename T>
struct evaluate : std::bool_constant<std::is_integral_v<T>>
{
static constexpr std::size_t size = sizeof(T); // Simplified for illustration only.
};
void foo() { }
template <typename T, typename... Args>
void foo( const T&, const Args&... args)
{
using X = evaluate<T>;
if constexpr ( X::value )
{
bar<X::size>();
}
else
{
hit<X::size>();
}
foo( args... );
}
int main() {
foo (5, "hello", true);
}
It "calls" only once evaluate<T>, which is not important but maybe easier to read. That all the template code is only used during instantiation makes it only a matter of taste.
As you mention c++17 you can use constexpr if to get rid of SFINAE at all in your example. This makes it also possible to reuse common lines of code in both variants of foo which is quite nice. The executable will not be much different you can believe, but the maintainability is much better I think!
Ok, I thought evaluate was computed twice. But how would you make it appear only once (without using macros)? We are supposed to avoid repetition in code anyways
You can try to save it as a additional template parameter with default value
Something as
template <typename T, typename... Args, typename E = evaluate<T>>
std::enable_if_t<!E::value> foo (const T&, const Args&...);
template <typename T, typename... Args, typename E = evaluate<T>>
std::enable_if_t<E::value> foo (const T&, const Args&... args)
{
bar<E::size>();
// Do whatever.
foo(args...);
}
template <typename T, typename ... Args, typename E>
std::enable_if_t<!E::value> foo (const T&, const Args&... args)
{
hit<E::size>();
// Do whatever, but different from the previous foo overload.
foo(args...);
}
Related
I implemented a Visit function (on a variant) that checks that the currently active type in the variant matches the function signature (more precisely the first argument). Based on this nice answer.
For example
#include <variant>
#include <string>
#include <iostream>
template<typename Ret, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
template <typename F>
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
template <typename T>
using first_argument = decltype(first_argument_helper(std::declval<T>()));
std::variant<int, std::string> data="abc";
template <typename V>
void Visit(V v){
using Arg1 = typename std::remove_const_t<std::remove_reference_t<first_argument<V>>>;//... TMP magic to get 1st argument of visitor + remove cvr, see Q 43526647
if (! std::holds_alternative<Arg1>(data)) {
std::cerr<< "alternative mismatch\n";
return;
}
v(std::get<Arg1>(data));
}
int main(){
Visit([](const int& i){std::cout << i << "\n"; });
Visit([](const std::string& s){std::cout << s << "\n"; });
// Visit([](auto& x){}); ugly kabooom
}
This works, but it explodes with a user unfriendly compile time error when users passes a generic (e.g. [](auto&){}) lambda. Is there a way to detect this and give nice static_assert() about it?
Would also be nice if it worked with function templates as well, not just with lambdas.
Note that I do not know what possible lambdas do, so I can not do some clever stuff with Dummy types since lambdas may invoke arbitrary functions on types.
In other words I can not try to call lambda in 2 std::void_t tests on int and std::string and if it works assume it is generic because they might try to call .BlaLol() on int and string.
Is there a way to detect this and give nice static_assert about it?
I suppose you can use SFINAE over operator() type.
Follows an example
#include <type_traits>
template <typename T>
constexpr auto foo (T const &)
-> decltype( &T::operator(), bool{} )
{ return true; }
constexpr bool foo (...)
{ return false; }
int main()
{
auto l1 = [](int){ return 0; };
auto l2 = [](auto){ return 0; };
static_assert( foo(l1), "!" );
static_assert( ! foo(l2), "!" );
}
Instead of a bool, you can return std::true_type (from foo() first version) or std::false_type (from second version) if you want to use it through decltype().
Would also be nice if it worked with function templates as well, not just with lambdas.
I don't think it's possible in a so simple way: a lambda (also a generic lambda) is an object; a template function isn't an object but a set of objects. You can pass an object to a function, not a set of objects.
But the preceding solution should works also for classes/structs with operator()s: when there is a single, non template, operator(), you should get 1 from foo(); otherwise (no operator(), more than one operator(), template operator()), foo() should return 0.
Yet another simpler option:
#include <type_traits>
...
template <typename V>
void Visit(V v) {
class Auto {};
static_assert(!std::is_invocable<V, Auto&>::value);
static_assert(!std::is_invocable<V, Auto*>::value);
...
}
The Auto class is just an invented type impossible to occur in the V parameters. If V accepts Auto as an argument it must be a generic.
I tested in coliru and I can confirm the solution covers these cases:
Visit([](auto x){}); // nice static assert
Visit([](auto *x){}); // nice static assert
Visit([](auto &x){}); // nice static assert
Visit([](auto &&x){}); // nice static assert
I'm not sure if that would cover all the possible lambdas that you don't know which are :)
#include <variant>
#include <string>
#include <iostream>
template <class U, typename T = void>
struct can_be_checked : public std::false_type {};
template <typename U>
struct can_be_checked<U, std::enable_if_t< std::is_function<U>::value > > : public std::true_type{};
template <typename U>
struct can_be_checked<U, std::void_t<decltype(&U::operator())>> : public std::true_type{};
template<typename Ret, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
template <typename F>
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
template <typename T>
using first_argument = decltype(first_argument_helper(std::declval<T>()));
std::variant<int, std::string> data="abc";
template <typename V>
void Visit(V v){
if constexpr ( can_be_checked<std::remove_pointer_t<decltype(v)>>::value )
{
using Arg1 = typename std::remove_const_t<std::remove_reference_t<first_argument<V>>>;//... TMP magic to get 1st argument of visitor + remove cvr, see Q 43526647
if (! std::holds_alternative<Arg1>(data))
{
std::cerr<< "alternative mismatch\n";
return;
}
v(std::get<Arg1>(data));
}
else
{
std::cout << "it's a template / auto lambda " << std::endl;
}
}
template <class T>
void foo(const T& t)
{
std::cout <<t << " foo \n";
}
void fooi(const int& t)
{
std::cout <<t << " fooi " << std::endl;
}
int main(){
Visit([](const int& i){std::cout << i << std::endl; });
Visit([](const std::string& s){std::cout << s << std::endl; });
Visit([](auto& x){std::cout <<x << std::endl;}); // it's a template / auto lambda*/
Visit(foo<int>);
Visit<decltype(fooi)>(fooi);
Visit(fooi);
// Visit(foo); // => fail ugly
}
I don't know if it's you want, but you can, with that static_assert if an auto lambda is passed as parameter.
I think it's not possible to do the same for template function, but not sure.
Is it possible to create a standalone template function which has a template parameter auto MEMFN (a member function pointer), and has the same return and parameter types as MEMFN has?
So, if MEMFN's type is
RETURN (OBJECT::*)(PARAMETERS...)
then the desired function is this:
template <auto MEMFN>
RETURN foo(OBJECT &, PARAMETERS...);
My problem is how to extract PARAMETERS... from MEMFN's type (RETURN and OBJECT are easy to do).
So I can call this function like this:
Object o;
foo<&Object::func>(o, <parameters>...);
As for request from n.m., here is a stripped down example from an actual code:
#include <utility>
template <typename RETURN, typename OBJECT, typename ...PARAMETERS>
struct Wrapper {
template <RETURN (OBJECT::*MEMFN)(PARAMETERS...)>
RETURN foo(PARAMETERS... parameters) {
// do whatever with MEMFN, parameters, etc. here, not part of the problem
}
};
struct Object {
template <auto MEMFN, typename RETURN, typename OBJECT, typename ...PARAMETERS>
RETURN call(OBJECT &&object, PARAMETERS &&...parameters) {
// here, MEMFN parameters and PARAMETERS must be the same
// Wrapper actually not created here, it is accessed by other means
Wrapper<RETURN, typename std::decay<OBJECT>::type, PARAMETERS...> w;
return w.template foo<MEMFN>(std::forward<PARAMETERS>(parameters)...);
}
};
struct Foo {
void fn(int);
};
int main() {
Object o;
Foo f;
o.call<&Foo::fn, void, Foo &, int>(f, 42);
// this is wanted instead:
// o.call<&Foo::fn>(f, 42);
}
Yes we can:
template <auto MemFn>
struct fooHelper;
template <typename Ret, typename Obj, typename ... Args, Ret (Obj::*MemFn)(Args...)>
struct fooHelper<MemFn>
{
static Ret call(Obj& obj, Args... args) {
return (obj.*MemFn)(args...);
}
};
template <auto MemFn, typename ... Args>
auto foo(Args ... args)
{
return fooHelper<MemFn>::call(args...);
}
Another way to define foo which doesn't introduce a brand new parameter pack is:
template <auto MemFn>
auto& foo = fooHelper<MemFn>::call;
Example usage:
#include <iostream>
struct moo
{
int doit (int x, int y) { return x + y; }
};
int main()
{
moo m;
std::cout << foo<&moo::doit>(m, 1, 2) << "\n";
}
(Perfect forwarding omitted for simplicity)
If you relax your demand on being standalone you can do something like:
#include <iostream>
template <auto MEMFN, class = decltype(MEMFN)>
struct S;
template <auto MEMFN, class Ret, class T, class... Args>
struct S<MEMFN, Ret (T::*)(Args...)> {
static Ret foo(T &o, Args... args) {
(o.*MEMFN)(args...);
}
};
struct A {
void foo(int a, int b) {
std::cout << a << " " << b << std::endl;
}
};
int main() {
A a;
S<&A::foo>::foo(a, 1, 2);
}
[live demo]
If no then you gonna have to have a patience to create a function overloads for each possible number of parameters:
#include <type_traits>
#include <tuple>
#include <iostream>
template <class, std::size_t>
struct DeduceParam;
template <class Ret, class T, class... Args, std::size_t N>
struct DeduceParam<Ret (T::*)(Args...), N> {
using type = std::tuple_element_t<N, std::tuple<Args...>>;
};
template <class>
struct DeduceResultAndType;
template <class Ret, class T, class... Args>
struct DeduceResultAndType<Ret (T::*)(Args...)> {
using result = Ret;
using type = T;
static constexpr decltype(sizeof(T)) size = sizeof...(Args);
};
template <auto MEMFN, class DRAT = DeduceResultAndType<decltype(MEMFN)>, std::enable_if_t<DRAT::size == 1>* = nullptr>
typename DRAT::result foo(typename DRAT::type o, typename DeduceParam<decltype(MEMFN), 0>::type param1) {
}
template <auto MEMFN, class DRAT = DeduceResultAndType<decltype(MEMFN)>, std::enable_if_t<DRAT::size == 2>* = nullptr>
typename DRAT::result foo(typename DRAT::type o, typename DeduceParam<decltype(MEMFN), 0>::type param1,
typename DeduceParam<decltype(MEMFN), 1>::type param2) {
}
struct A {
void foo(int a, int b) {
std::cout << a << " " << b << std::endl;
}
};
int main() {
A a;
foo<&A::foo>(a, 1, 2);
}
I'd like to define functions for integral types, string and other types.
I can write:
template<typename T, typename = std::enable_if<std::is_integral<T>::value>::type>
void foo();
template<typename T, typename = std::enable_if<std::is_same<std::string>::value>::type>
void foo();
But how I can define function that will be called in other cases (if T not integral type and not std::string)?
I'm pretty sure that writing something like the line below becomes quite annoying and error-prone when you want to write up to N sfinae'd versions of foo:
std::enable_if<!std::is_integral<T>::value && !std::is_same<T, std::string>::value>::type
To avoid it, you can use the choice trick (a simple way to exploit overload resolution actually).
It follows a minimal, working example:
#include <iostream>
#include <utility>
#include <string>
template<int N> struct Choice: Choice<N-1> {};
template<> struct Choice<0> {};
template<typename T, typename... Args>
std::enable_if_t<std::is_integral<T>::value>
bar(Choice<2>, Args&&...) { std::cout << "integral" << std::endl; }
template<typename T, typename... Args>
std::enable_if_t<std::is_same<T, std::string>::value>
bar(Choice<1>, Args&&...) { std::cout << "string" << std::endl; }
template<typename T, typename... Args>
void bar(Choice<0>, Args&&...) { std::cout << "whatever" << std::endl; }
template<typename T, typename... Args>
void foo(Args&&... args) { bar<T>(Choice<100>{}, std::forward<Args>(args)...); }
int main() {
foo<bool>("foo");
foo<std::string>(42);
foo<void>(.0, "bar");
}
It handles nicely also the arguments that are directly forwarded to the right function once it has been picked up from the set.
The basic idea is that it tries to use all the versions of your function in the order you specified, from N to 0. This has also the advantage that you can set a priority level to a function when two of them match the template parameter T with their sfinae expressions.
The sfinae expressions enable or disable the i-th choice and you can easily define a fallback by using the Choice<0> tag (that is far easier to write than std::enable_if<!std::is_integral<T>::value && !std::is_same<T, std::string>::value>::type).
The drawback (even though I wouldn't consider it a drawback) is that it requires an extra function that simply forwards the arguments to the chain by appending them to the Choice<N> tag.
See it up and running on Coliru.
Your examples do not compile at all for many reasons. See proper SFINAE in the code below. This is only one of many possible ways to do it.
You can just negate all special conditions simultaneously. For example:
template<typename T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
void foo() { std::cout << "Integral\n"; }
template<typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr>
void foo() { std::cout << "Str\n"; }
template<typename T, std::enable_if_t<!std::is_integral<T>::value && !std::is_same<T, std::string>::value>* = nullptr>
void foo() { std::cout << "Something else\n"; }
int main(void)
{
foo<int>();
foo<std::string>();
foo<float>();
return 0;
}
prints:
Integral
Str
Something else
Note that you may get automatic overload resolution if your functions take template-dependent arguments. SFINAE will look a bit different in this case:
template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type foo(const T&) { std::cout << "Integral\n"; }
template<typename T>
typename std::enable_if<std::is_same<T, std::string>::value>::type foo(const T&) { std::cout << "Str\n"; }
template<typename T>
typename std::enable_if<!std::is_integral<T>::value && !std::is_same<T, std::string>::value>::type
foo(const T&) { std::cout << "Something else\n"; }
Usage:
foo(1);
foo(std::string("fdsf"));
foo(1.1f);
Finally, in the last case std::string overload may be on-template function:
template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type foo(const T&) { std::cout << "Integral\n"; }
template<typename T>
typename std::enable_if<!std::is_integral<T>::value && !std::is_same<T, std::string>::value>::type
foo(const T&) { std::cout << "Something else\n"; }
void foo(const std::string&) { std::cout << "Str\n"; }
#Jarod42 was right.
I chose the wrong way to use SFINAE.
Actually, I had to use std::enable_if in return type declaration. And in order to define 'default' function (for all other types) I just need to define function with the same name and with ... as input parameters.
template<typename T>
std::enable_if_t<std::is_integral<T>::value>
foo() { std::cout << "integral"; }
template<typename T>
std::enable_if_t<std::is_same<T, std::string>::value>
foo() { std::cout << "string"; }
template<typename T>
void foo(...) { std::cout << "other"; }
The CUDA 7 standard regarding variadic global function templates states "only a single pack parameter is allowed." Is there an elegant workaround for this? I want to be able to do something like:
template<int... vals>
void RecursiveFunct() {
}
template<int... vals, typename T, typename... Args>
void RecursiveFunct(T t, Args... args) {
t.template call<vals...>();
RecursiveFunct<vals...>(args...);
}
I'm thinking I could wrap my pack of integers into something before passing them along but is it possible to do that in a way that is transparent to the caller of this code?
Not sure to understand your exactly limits but... I suppose that std::integer_sequence and a wrapper function to call call() can help you.
The following is a toy, but compilable, example that show what I mean.
struct foo
{
template <int ... vals>
void call () const
{ std::cout << "- call " << sizeof...(vals) << std::endl; }
};
template <typename IS>
void RecursiveFunct (IS const &)
{ }
template <typename T, int ... vals>
void wrapCall (T const & t, std::integer_sequence<int, vals...> const &)
{ t.template call<vals...>(); }
template<typename IS, typename T, typename ... Args>
void RecursiveFunct (IS const & is, T t, Args... args)
{
wrapCall(t, is);
RecursiveFunct(is, args...);
}
int main ()
{
// print 5 times "- call 4"
RecursiveFunct(std::integer_sequence<int, 2, 3, 5, 7>{},
foo{}, foo{}, foo{}, foo{}, foo{});
}
Take in count that std::integer_sequence is a C++14 feature, so the preceding code needs (at least) a C++14 compiler.
But if you need to work with C++11, it's trivial to create a std::integer_sequence substitute.
By example
template <typename T, T ... ts>
struct myIntegerSequence
{ };
-- EDIT --
The OP ask
can this work without creating an instance of integer_sequence?
In normal C++14, yes. Works this with Cuda? I don't know.
I've obtained this changing the wrapCall() func with a wrapCall struct and a func() static method. This because I've used the partial specialization that can't be used with funcs.
The folling is the toy example
#include <utility>
#include <iostream>
struct foo
{
template <int ... vals>
void call () const
{ std::cout << "- call " << sizeof...(vals) << std::endl; }
};
template <typename>
void RecursiveFunct ()
{ }
template <typename>
struct wrapCall;
template <int ... vals>
struct wrapCall<std::integer_sequence<int, vals...>>
{
template <typename T>
static constexpr void func (T const & t)
{ t.template call<vals...>(); }
};
template<typename IS, typename T, typename ... Args>
void RecursiveFunct (T t, Args... args)
{
wrapCall<IS>::func(t);
RecursiveFunct<IS>(args...);
}
int main ()
{
// print 5 times "- call 4"
RecursiveFunct<std::integer_sequence<int, 2, 3, 5, 7>>
(foo{}, foo{}, foo{}, foo{}, foo{});
}
But are you sure that is a problem the istantiation of a std::integer_sequence?
I have the following code :
template<size_t sz,typename T=float> class Vec{
T v[sz];
Vec(const T& val,const T&... nv){
//how do i assign `sz` number of first arguments into `this->v` array
}
}
I want to create constructor, that receive generic number of constructor argument, and assign the first sz number of arguments into member variable of v
what I want to do, is to be able doing like this: Vec<3> var(1.0,2.0,3.0);
This is possible, but complicated. Here is some code that does it. It may be possible to eliminate the holder type, but I leave that as an exercise for the reader. This has been tested with g++ 4.6.
#include <iostream>
#include <typeinfo>
template<size_t ... Indices> struct indices_holder
{};
template<size_t index_to_add,typename Indices=indices_holder<> >
struct make_indices_impl;
template<size_t index_to_add,size_t...existing_indices>
struct make_indices_impl<index_to_add,indices_holder<existing_indices...> >
{
typedef typename make_indices_impl<
index_to_add-1,
indices_holder<index_to_add-1,existing_indices...> >::type type;
};
template<size_t... existing_indices>
struct make_indices_impl<0,indices_holder<existing_indices...> >
{
typedef indices_holder<existing_indices...> type;
};
template<size_t max_index>
typename make_indices_impl<max_index>::type make_indices()
{
return typename make_indices_impl<max_index>::type();
}
template<unsigned index,typename ... U>
struct select_nth_type;
template<unsigned index,typename T,typename ... U>
struct select_nth_type<index,T,U...>
{
typedef typename select_nth_type<index-1,U...>::type type;
static type&& forward(T&&,U&&... u)
{
return select_nth_type<index-1,U...>::forward(static_cast<U&&>(u)...);
}
};
template<typename T,typename ... U>
struct select_nth_type<0,T,U...>
{
typedef T type;
static type&& forward(T&&t,U&&...)
{
return static_cast<T&&>(t);
}
};
template<unsigned index,typename ... U>
typename select_nth_type<index,U...>::type&& forward_nth(U&&... u)
{
return static_cast<typename select_nth_type<index,U...>::type&&>(
select_nth_type<index,U...>::forward(
static_cast<U&&>(u)...));
}
template<size_t sz,typename T=float> struct Vec{
struct holder
{
T data[sz];
};
holder v;
template<typename ... U>
struct assign_helper
{
template<size_t... Indices>
static holder create_array(indices_holder<Indices...>,Vec* self,U&&... u)
{
holder res={{static_cast<T>(forward_nth<Indices>(u...))...}};
return res;
}
};
template<typename ... U>
Vec(U&&... u):
v(assign_helper<U...>::create_array(make_indices<sz>(),this,static_cast<U&&>(u)...))
{}
};
int main()
{
Vec<3> v(1.2,2.3,3.4,4.5,5.6,7.8);
std::cout<<"v[0]="<<v.v.data[0]<<std::endl;
std::cout<<"v[1]="<<v.v.data[1]<<std::endl;
std::cout<<"v[2]="<<v.v.data[2]<<std::endl;
}
I believe this satisfies all the requirements:
template <size_t sz,typename T,typename... Args> struct Assign;
template <typename T,typename First,typename...Rest>
struct Assign<1,T,First,Rest...> {
static void assign(T *v,const First &first,const Rest&... args)
{
*v = first;
}
};
template <size_t sz,typename T,typename First,typename... Rest>
struct Assign<sz,T,First,Rest...> {
static void assign(T *v,const First &first,const Rest&... rest)
{
*v = first;
Assign<sz-1,T,Rest...>::assign(v+1,rest...);
}
};
template<size_t sz,typename T=float>
struct Vec{
T v[sz];
template <typename... Args>
Vec(const T& val,const Args&... nv){
Assign<sz,T,T,Args...>::assign(v,val,nv...);
}
};
This is another technique which is much simpler if it is ok not to have at least sz parameters:
template<size_t sz,typename T=float>
struct Vec {
T v[sz];
template <typename... Args>
Vec(const T& val,const Args&... nv)
{
T data[] = {val,static_cast<const T &>(nv)...};
int i=0;
for (; i<sz && i<(sizeof data)/sizeof(T); ++i) {
v[i] = data[i];
}
for (; i<sz; ++i) {
v[i] = T();
}
}
};
First declare this utility function:
template <typename T> inline void push(T* p) {}
template <typename T, typename First, typename... Args>
inline void push(T* p, First&& first, Args&&... args)
{
*p = first;
push(++p, std::forward<Args>(args)...);
}
Then, in your class:
template<size_t sz,typename T=float>
class Vec
{
T v[sz];
template <typename... Args>
Vec(T first, Args&&... args) // << we have changed const T& to T&&
{
//how do i assign `sz` number of first arguments into `this->v` array
// like this:
push(&v[0], first, std::forward<Args>(args)...);
}
}
sz is a template argument, you can directly use it in your code.
The following could work:
template <typename T, std::size_t N>
struct Foo
{
T arr[N];
template <typename ...Args> Foo(Args &&... args) : arr{std::forward<Args>(args)...} { }
};
Usage:
Foo<int, 3> a(1,2,3);
This allows you to construct the array elements from anything that's convertible to T. You can obtain the number of parameters (which can be anything not exceeding N) with sizeof...(Args).
Can you make use of something like this?
template <typename... Args> class Vec {
std :: tuple <Args...> m_args;
Vec (const Foo & a, const Bar & b, Args&&... args)
: m_args (args...)
{
// ... something with a, b
}
};
There are a few rules to constrain you:
you can only have one template... Args-style packed argument list per template class
methods which use it must have the templated arguments on the right-hand-side
You need to unpack the argument pack while keeping a count and do the necessary runtime operation in a functor. This should get you started:
template<unsigned, typename...>
struct unroll;
template<unsigned size, typename Head, typename... Tail>
struct unroll<size, Head, Tail...> {
void operator()(Head&& h, Tail&&... tail) {
// do your stuff, pass necessary arguments through the ctor of the
// struct
unroll<size - 1, Tail...>()(std::forward<Tail>(tail)...);
}
};
template<typename Head, typename... Tail>
struct unroll<1, Head, Tail...> {
void operator()(Head&& h, Tail&&... tail) {
// do your stuff the last time and do not recurse further
}
};
int main()
{
unroll<3, int, double, int>()(1, 3.0, 2);
return 0;
}
The following almost works (and for the last N arguments instead of the first, but hey). Perhaps someone can help with the compile error in the comments below:
#include <iostream>
void foo (int a, int b) {
std :: cout << "3 args: " << a << " " << b << "\n";
}
void foo (int a, int b, int c) {
std :: cout << "3 args: " << a << " " << b << " " << c << "\n";
}
template <int n, typename... Args>
struct CallFooWithout;
template <typename... Args>
struct CallFooWithout <0, Args...> {
static void call (Args... args)
{
foo (args...);
}
};
template <int N, typename T, typename... Args>
struct CallFooWithout <N, T, Args...> {
static void call (T, Args... args)
{
CallFooWithout <N-1, Args...> :: call (args...);
// ambiguous class template instantiation for 'struct CallFooWithout<0, int, int, int>'
// candidates are: struct CallFooWithout<0, Args ...>
// struct CallFooWithout<N, T, Args ...>
}
};
template <int n, typename... Args>
void call_foo_with_last (Args... args)
{
CallFooWithout <sizeof...(Args)-n, Args...> :: call (args...);
}
int main ()
{
call_foo_with_last <2> (101, 102, 103, 104, 105);
call_foo_with_last <3> (101, 102, 103, 104, 105);
}
I don't see why it's ambiguous because 0 is more specialised than N so that should satisfy the partial order ?!?!?
By contrast, the below is fine.
template <int N, typename... T>
struct Factorial
{
enum { value = N * Factorial<N - 1,T...>::value };
};
template <typename... T>
struct Factorial<0, T...>
{
enum { value = 1 };
};
void foo()
{
int x = Factorial<4,int>::value;
}
What's the difference?