variadic-templates

Simple variadic template function can't instantinate

£可爱£侵袭症+ 提交于 2019-12-18 06:59:24
问题 I'm aware that sizeof...(Args...) yields the number of types in a C++0x packed template argument list, but I wanted to implement it in terms of other features for demonstation purposes, but it won't compile. // This is not a solution -- overload ambiguity. // template <typename... Args> size_t num_args (); // Line 7 // template <> constexpr size_t num_args () { return 0; } template <typename H, typename... T> constexpr size_t num_args () // Line 16 { return 1 + num_args <T...> (); // *HERE* }

Create a type list combination of types in C++

眉间皱痕 提交于 2019-12-18 05:44:18
问题 Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or C. For a N=2 case, this would be std::tuple<A,A> std::tuple<A,B> std::tuple<A,C> std::tuple<B,A> std::tuple<B,B> std::tuple<B,C> std::tuple<C,A> std::tuple<C,B> std::tuple<C,C> The idea is to create a tuple which holds a container for all those types,

Passing std::array as arguments of template variadic function

孤街浪徒 提交于 2019-12-18 04:48:06
问题 I am trying to learn about variadic templates in C++11. I have a class which is basically a wrapper around a std::array . I want to be able to pass function objects (ideally lambdas) to a member function and then have the elements of the std::array passed on as parameters of the function object. I have used a static_assert to check that the number of parameters matches the length of the array but I cannot think of a way to pass the elements as arguments. Here is the code #include <iostream>

Create n-dimensional vector with given sizes

陌路散爱 提交于 2019-12-18 04:10:55
问题 So, what I want is to create multidimensional vector of given type where the first dimension will have size of the first argument of a function call, etc, for example if I do std::size_t n = 5; auto x = make_vector<int>(n + 1, n * 2, n * 3); x should be 6x10x15 3d array (consisting of zeroes, because I want to default construct right now) I have tried this: template <typename T> std::vector<T> make_vector(std::size_t size) { return std::vector<T>(size); } template <typename T, typename...

Create n-dimensional vector with given sizes

回眸只為那壹抹淺笑 提交于 2019-12-18 04:10:01
问题 So, what I want is to create multidimensional vector of given type where the first dimension will have size of the first argument of a function call, etc, for example if I do std::size_t n = 5; auto x = make_vector<int>(n + 1, n * 2, n * 3); x should be 6x10x15 3d array (consisting of zeroes, because I want to default construct right now) I have tried this: template <typename T> std::vector<T> make_vector(std::size_t size) { return std::vector<T>(size); } template <typename T, typename...

Unpacking arguments from tuples

早过忘川 提交于 2019-12-18 04:09:23
问题 So I'm trying to figure out how this works: C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args? The piece of black magic I do not understand is this code fragment: f(std::get<N>(std::forward<Tuple>(t))...) it's the expression inside f that I don't understand. I understand that the expression somehow unpacks/expands what's inside t into a list of arguments. But could someone care to explain how this is done? When I look at the definition of std::get (http:/

How to write a variadic template recursive function?

自闭症网瘾萝莉.ら 提交于 2019-12-18 03:55:21
问题 I'm trying to write a variadic template constexpr function which calculates sum of the template parameters given. Here's my code: template<int First, int... Rest> constexpr int f() { return First + f<Rest...>(); } template<int First> constexpr int f() { return First; } int main() { f<1, 2, 3>(); return 0; } Unfortunately, it does not compile reporting an error message error C2668: 'f': ambiguous call to overloaded function while trying to resolve f<3,>() call. I also tried to change my

Ambiguous call when recursively calling variadic template function overload

ぃ、小莉子 提交于 2019-12-18 03:42:12
问题 Consider this piece of code: template<typename FirstArg> void foo() { } template<typename FirstArg, typename... RestOfArgs> void foo() { foo<RestOfArgs...>(); } int main() { foo<int, int, int>(); return 0; } It does not compile due to ambiguous call foo<RestOfArgs...>(); when RestOfArgs has only one element ( {int} ). But this compiles without error: template<typename FirstArg> void foo(FirstArg x) { } template<typename FirstArg, typename... RestOfArgs> void foo(FirstArg x, RestOfArgs... y) {

Ambiguous call when recursively calling variadic template function overload

风格不统一 提交于 2019-12-18 03:41:09
问题 Consider this piece of code: template<typename FirstArg> void foo() { } template<typename FirstArg, typename... RestOfArgs> void foo() { foo<RestOfArgs...>(); } int main() { foo<int, int, int>(); return 0; } It does not compile due to ambiguous call foo<RestOfArgs...>(); when RestOfArgs has only one element ( {int} ). But this compiles without error: template<typename FirstArg> void foo(FirstArg x) { } template<typename FirstArg, typename... RestOfArgs> void foo(FirstArg x, RestOfArgs... y) {

How to implement folding with variadic templates

别等时光非礼了梦想. 提交于 2019-12-18 03:33:26
问题 I have an almost working solution. However, it fails to compile some simple cases, and I can't decipher the error message. My current solution: #define AUTO_RETURN( EXPR ) -> decltype( EXPR ) \ { return EXPR; } template< typename BinaryFunc, typename First, typename Second > auto foldl( BinaryFunc&& func, First&& first, Second&& second ) AUTO_RETURN( func( std::forward<First>(first), std::forward<Second>(second) ) ) template<typename BinaryFunc, typename First, typename Second, typename...