variadic-templates

Why does std::visit take a variable number of variants?

泄露秘密 提交于 2019-12-04 22:30:37
Trying to get more familiar with C++17, I've just noticed std::visit : template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple visitors and using them on the same variant. So, why this specific 'variadification'? To make multiple

Can a parameter pack be captured implicitly within C++11 lambdas?

自古美人都是妖i 提交于 2019-12-04 22:24:06
Does anyone know if the following implicit capture of 'ts' is well-formed: template<class ... Ts> void bar(Ts ... ts) { } template<class ... Ts> int foo(Ts ... ts) { auto L = [=] () { bar(ts...); }; L(); return 0; } int g = foo(1, 2, 3); Does the standard clearly state anywhere that this should not be well formed? 14.5.3/6: The instantiation of a pack expansion that is not a sizeof... expression produces a list E1, E2, ..., EN , where N is the number of elements in the pack expansion parameters. Each Ei is generated by instantiating the pattern and replacing each pack expansion parameter with

C++ Variadic template AND and OR

巧了我就是萌 提交于 2019-12-04 22:21:00
问题 Can you use C++11 variadic templates to complete /* ??? */ in: template<bool...v> struct var_and { static bool constexpr value = /* ??? */; }; so that var_and<v...>::value provides && over the boolean pack v at compile-time? Can you do the same for struct var_or<v...> for || ? Can you use short-circuit evaluation (in both cases)? Edit : An update to the accepted answer added that C++17 fold expressions enable template<bool... v> constexpr bool var_and = (v && ...); template<bool... v>

Permutation P(N,R) of types in compile time

流过昼夜 提交于 2019-12-04 18:48:25
I've written a working code for computing P(N,R) for packs when the pack has all different types, e.g. PermutationN<2, P<int, char, bool>> is to be P< P<int, char>, P<int, bool>, P<char, int>, P<char, bool>, P<bool, int>, P<bool, char> > But when there are repeat elements, I'm getting the wrong results. For example, PermutationN<2, P<int, int, char>> should be P< P<int, int>, P<int, char>, P<char, int> > Here is my working code for when all the types are different. I'm stuck on how to adapt it so that it gives correct results when there are repeated types in the pack. Any help would be

Compiletime for each with custom functions

*爱你&永不变心* 提交于 2019-12-04 18:39:00
Abstract: Imagine a problem of the following form: One has to invoke multiple specific member functions with the same parameters on a list of functors. That makes a good problem to solve with an interface (runtime_interface, in other words a requirement of functions that those functors have to implement). The Problem I would like to discuss is the case where the list of functors is known at compile time, but might be subject to change during the further development process. Because in this case if implemented like that one is paying the runtime overhead even though all the functions to be

Calculate the average of several values using a variadic-template function

那年仲夏 提交于 2019-12-04 18:34:31
I am trying to write a function to determine the average of an arbitrary number of arguments, all of which have the same type. For learning purposes I am trying to do this using a variadic-templated function. This is what I have so far: template<typename T, class ... Args> T Mean(Args ... args) { int numArgs = sizeof...(args); if (numArgs == 0) return T(); // If there are no arguments, just return the default value of that type T total; for (auto value : args...) { total += value; } return total / numArgs; // Simple arithmetic average (sum divided by total) } When I try to compile this (using

For constructors, how do I choose between variadic-templates vs std::initializer_list?

╄→гoц情女王★ 提交于 2019-12-04 18:05:25
问题 In the current state of c++11 (say gcc 4.7.2), how should I choose between using a variadic-template or a std::initializer_list when I need a constructor that can take variable arguments? 回答1: A variadic template allows you providing arguments of different types, while an std::initializer_list is templated with the type of the argument. This means the type of all the elements in the list must be the same (or convertible to the underlying type, but no narrowing conversions are allowed).

How do you static_assert the values in a parameter pack of a variadic template?

血红的双手。 提交于 2019-12-04 17:37:45
问题 I'm creating a variadic template. Let's say I have something like this: template<typename T, T ... Numbers> class Sequence final { // Unpack parameter pack into a constexpr array constexpr static T count = sizeof...(Numbers); constexpr static T numbers[count] = { Numbers... }; // ... } Instances of this class can be instantiated like: Sequence<uint32_t, 1, 2, 3, 42, 25> seq; I'd like to make sure at compile time using a static_assert that the numbers parameter pack only contains specific

Scalability of variadic templates

拈花ヽ惹草 提交于 2019-12-04 17:05:14
问题 I am working on a large-scale software infrastructure in C++11 that makes extensive use of variadic templates. My question is the following: what is the scalability of this approach? First, is there an upper limit in the number of arguments that variadic templates can take? Second, is code bloat a major issue with state-of-the-art compilers when many arguments are used (and, by extension, many combinations of these arguments that would yield to many different implementations of the templated

Class template specialization priority/ambiguity

ε祈祈猫儿з 提交于 2019-12-04 16:20:25
问题 While trying to implement a few things relying on variadic templates, I stumbled accross something I cannot explain. I boiled down the problem to the following code snippet: template <typename ... Args> struct A {}; template <template <typename...> class Z, typename T> struct test; template <template <typename...> class Z, typename T> struct test<Z, Z<T>> { static void foo() { std::cout << "I'm more specialized than the variadic spec, hehe!" << std::endl; } }; template <template <typename...>