variadic-templates

Is the order for variadic template pack expansion defined in the standard?

守給你的承諾、 提交于 2019-12-21 17:27:03
问题 I thought that expanding a parameter pack had the following behavior: // for Args ... p f(p)...; // was equivalent to f(p1); f(p2); ...; f(pn); But I just found out that gcc (4.6, 4.7 and 4.8) does it the other way around: f(pn); ...; f(p2); f(p1); Whereas clang does it as I expected. Is that a bug in GCC or are they both valid according to the standard? Minimal example #include <iostream> #include <string> template<typename T> bool print(const unsigned index, const T& value){ std::cerr <<

Sequencing among a variadic expansion

混江龙づ霸主 提交于 2019-12-21 15:42:33
问题 For this non-variadic example: int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } it's not specified whether Func1() or Func2() is computed first, just that both must be done before MyFunc() is called. How does this sequencing work with the expansion of variadic arguments? template < typename Func, typename ...Args > void MyFunc2( Func &&f, Args&& ...a ) { int b[] = { f( std::forward<Args>(a) )... }; //... } Let's say that f is a function

Sequencing among a variadic expansion

亡梦爱人 提交于 2019-12-21 15:39:28
问题 For this non-variadic example: int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } it's not specified whether Func1() or Func2() is computed first, just that both must be done before MyFunc() is called. How does this sequencing work with the expansion of variadic arguments? template < typename Func, typename ...Args > void MyFunc2( Func &&f, Args&& ...a ) { int b[] = { f( std::forward<Args>(a) )... }; //... } Let's say that f is a function

C++ Convert a parameter pack of types to parameter pack of indices

↘锁芯ラ 提交于 2019-12-21 12:42:56
问题 Is there any way to convert a parameter pack of types to a parameter pack of integers from 0 to sizeof...(Types) ? More specifically, I'm trying to do something this this: template <size_t... I> void bar(); template <typename... Types> void foo() { bar<WHAT_GOES_HERE<Types>...>(); } For example, foo<int,float,double>() should call bar<0, 1, 2>() ; In my use case the parameter pack Types may contain the same type multiple times, so I cannot search the pack to compute the index for a given type

For every template type an argument of a set type

我与影子孤独终老i 提交于 2019-12-21 12:41:14
问题 Say I have a variadic template class. How do I create a function such that it's arguments are of a set type, for example int , with the number of arguments being equal to the number of template types? template <typename... Types> class Test { public: void Func(???); // I don't know how to declare such a function } Test<string, bool, long> myTest; // Three types myTest.Func(905, 36, 123315); // Three arguments, but always of type int. In the end, the goal of the function is to return a tuple

Forwarding arguments to template member function

点点圈 提交于 2019-12-21 07:34:04
问题 ideone example I need to forward some predefined arguments plus some user-passed arguments to a member function. #define FWD(xs) ::std::forward<decltype(xs)>(xs) template<class T, class... Ts, class... TArgs> void forwarder(void(T::*fptr)(Ts...), TArgs&&... xs) { T instance; (instance.*fptr)(FWD(xs)..., 0); // ^ // example predefined argument } forwarder(&example::f0, 10, 'a'); forwarder(&example::f1, 10, "hello", 5); This works properly for non-template member functions. The member function

g++ and clang++ different behaviour deducing variadic template `auto` values

半城伤御伤魂 提交于 2019-12-21 07:22:36
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

g++ and clang++ different behaviour deducing variadic template `auto` values

南楼画角 提交于 2019-12-21 07:21:47
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

Variadic template as template parameter: deduction works with GCC but not with Clang

两盒软妹~` 提交于 2019-12-21 06:51:00
问题 While compiling some C++11 code with both GCC 4.7.2 and Clang 3.1, I ran into a problem with Clang not managing to deduce a template argument where GCC succeeds. In a more abstract form, the code looks like this: src/test.cc: struct Element { }; template <typename T> struct FirstContainer { }; template <typename T, typename U = Element> struct SecondContainer { }; template <template <typename> class Container> void processOrdinary(Container<Element> /*elements*/) { } template <template

Why template instantiations go on forever here?

心不动则不痛 提交于 2019-12-21 06:39:22
问题 In the following code, I want to replace template <typename T, typename... Args> auto check (rank<1,T>, Args... args) const -> std::enable_if_t<!has_argument_type<T, Args...>(), decltype(check(rank<2, Ts...>{}, args...))> { return check(rank<2, Ts...>{}, args...); // Since rank<1,T> derives immediately from rank<2, Ts...>. } template <typename T, typename... Args> auto check (rank<2,T>, Args... args) const -> std::enable_if_t<!has_argument_type<T, Args...>(), decltype(check(rank<3, Ts...>{},