variadic-templates

How to extract lambda's Return Type and Variadic Parameters Pack back from general template<typename T>

纵饮孤独 提交于 2019-12-04 10:07:52
I want to create a templated class or function, that receives a lambda, and puts it internally in std::function<> Lambda could have any number of input parameters [](int a, float b, ...) std::function<> should correspond to the lambda's operator()'s type template <typename T> void getLambda(T t) { // typedef lambda_traits::ret_type RetType; ?? // typedef lambda_traits::param_tuple --> somehow back to parameter pack Args... std::function<RetType(Args...)> fun(t); } int main() { int x = 0; getLambda([&x](int a, float b, Person c){}); } So I need to somehow extract the Return Type and Parameter

Enforce variadic template of certain type

烈酒焚心 提交于 2019-12-04 10:05:35
问题 I would like to enforce the type of variadic template to be identical to an earlier set template type. In the below example, I'd like T and U to be the same type. code on ideone.com #include <iostream> #include <string> template<class T> struct Foo { Foo(T val) { std::cout << "Called single argument ctor" << std::endl; // [...] } // How to enforce U to be the same type as T? template<class... U> Foo(T first, U... vals) { std::cout << "Called multiple argument ctor" << std::endl; // [...] } };

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

戏子无情 提交于 2019-12-04 09:38:55
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. In C++14 you can use std::index_sequence_for from the <utility> header along with tagged dispatch.

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

非 Y 不嫁゛ 提交于 2019-12-04 09:33:41
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 << "Parameter " << index << " is " << value << std::endl; return true; } template<typename ... Args> void

Template specialization for an empty parameter pack

安稳与你 提交于 2019-12-04 09:10:07
问题 I have a variadic template function which calls itself to determine the largest number in a list (constituted by the templatized arguments). I am trying to make a specialization for when the parameter pack is empty so I can just return the number at the front of the list, but I don't know how to do that. I am just becoming familiar with variadic templates and template specialization, but this is what I have so far: #include <string> #include <iostream> using namespace std; template <int N,

Parameter packs not expanded with '…'

十年热恋 提交于 2019-12-04 08:31:07
问题 I have this code: #include <iostream> using namespace std; int print(int i) { cout << endl << i; } template<typename ...Args> inline void pass(Args&&...args) { } template<typename ...args> inline void expand(args&&... a) { print(a) ...; //this doesn't expand //pass( print(a)... ); this works } int main() { expand(1,2,3,4); return 0; } It throws an error: In function 'void expand(args&& ...)': error: expected ';' before '...' token print(a) ...; ^ parameter packs not expanded with '...': print

g++ and clang++ (with libc++) different behaviour with template template class specialization

无人久伴 提交于 2019-12-04 08:15:49
I'm playing with c++11 and I came across a difference in behavior between g++ 4.9.2 and clang++ 3.5 (but only when it uses the libc++; when it uses the libstdc++, clang++ seems to behave as such as g++) related with a template template specialization for a template class. The following is a trivial example #include <set> template <typename X> class foo; template <template<typename, typename ...> class C, typename X> class foo<C<X>> {}; int main () { foo<std::set<int>> f; return 0; } g++ compiles without problems; clang++ compiles without problems with the libstdc++ but, adding -stdlib=libc++ ,

Writing variadic template constructor

Deadly 提交于 2019-12-04 08:04:53
问题 Recently I asked this question but now I would like to expand it. I wrote the following class: template <class T> class X{ public: vector<T> v; template <class T> X(T n) { v.push_back(n); } template <class T, class... T2> X(T n, T2... rest) { v.push_back(n); X(rest...); } }; When creating an object using X<int> obj(1, 2, 3); // obj.v containts only 1 Vector only contains the first value, but not others. I've checked and saw that constructor is called 3 times, so I'm probably creating temp

C++ variadic template template argument that matches any kind of parameters

随声附和 提交于 2019-12-04 07:43:01
问题 I was wondering if it's possible to write a template function that can take any other arbitrary template as a parameter and properly match the template name (i.e. not just the resulting class). What I know to work is this: template<template<typename ...> class TemplateT, typename... TemplateP> void f(const TemplateT<TemplateP...>& param); Which will match for instance for f(std::vector<int>()) or f(std::list<int>()) but will not work for f(std::array<int, 3>()) , as the second parameter is a

Multikey map using variadic templates

点点圈 提交于 2019-12-04 06:31:40
I'm trying to implement a map with different access keys using variadic templates in c++. What I want to get is to make such syntax work: MultikeyMap<int, double, float> map1; // int and double are keys, float is value type map1[ 2 ] = 3.5; map1[ 5.7 ] = 22; MultikeyMap<unsigned long long, int, float, double, int> map2; // more keys, int is value type map2[100000000000ULL] = 56; // etc... What I have now looks like: template<class V, class... Krest> class MultikeyMap; template<class V, class K, class... Krest> class MultikeyMap<V, K, Krest...> : protected std::map<K, V>, protected MultikeyMap