variadic-templates

Default initialized (with value initialization) parameter pack

孤人 提交于 2019-12-10 22:09:03
问题 Can I default initialize a parameter pack to the respective value initialization of each type ? To elaborate a bit more, take the example of a simple function template template<typename T> void f(T arg = T()) { // eg for T=int, arg is 0 (value initialization) when default initialized } Would it be possible to express its variadic counterpart, ie template<typename... Args> void F(Args... args /* how can I value initialize the parameter pack? */) { } 回答1: #include <iostream> #include <utility>

zero length variadic expansion of ill-formed call

佐手、 提交于 2019-12-10 21:46:38
问题 Question for standard gurus. Trying to respond to another question, I came to doubt about the well-formedness of a code. As far I know, the following code is ill-formed int main () { std::tuple<> a; std::get<0>(a); } because a call to std::get<I>(t) , when t is a std::tuple<Ts...> , is ill-formed when I is outside the range [0, sizeof...(Ts)[ . In this case sizeof...(Ts) is zero, so the range [0, 0[ is empty, so std::get<I>(a) is ill-formed for every index I . But when std::get<I>(a) is

how should I call all functions in a variadic parameter pack if the function return type is void?

白昼怎懂夜的黑 提交于 2019-12-10 21:02:07
问题 I have a parameter pack full of default constructable and then callable objects (like the ExampleFunctor ) and want to call all of them in order (left to right). If the return type is anything besides void I can use an initializer list to do this: struct ExampleFunctor{ void operator()(){someGlobal = 4;} }; template<typename... Ts> struct CallThem { void operator()(){ auto list = {Ts()()...}; } } however if the return type is void this trick does not work. I could wrap all the Ts in a wrapper

Call function for each tuple element on one object without recursion

时光毁灭记忆、已成空白 提交于 2019-12-10 20:27:18
问题 I have an object of class A that may be called with different types and returns changed self on each call. For purpose of this question A will do struct A { A call(const int&) { } A call(const string& s) { } //// } a; So I have a tuple of unknown types: std::tuple<Types...> t; and I want to call a with each tuple element, so I want to get something like: b = a; b = b.call(get<0>(t)); b = b.call(get<1>(t)); b = b.call(get<2>(t)); //... or b = a.call(get<0>(t)).call(get<1>(t)).call(get<2>(t)...

how to assing multiple std::tuple_element as function arguments using variadic size_t templates

*爱你&永不变心* 提交于 2019-12-10 19:15:38
问题 I want to create a function that changes multiple values inside a tuple with one call. template<class... Args> class EventN { public: std::tuple<Args...> mArgs; EventN(Args... args) : mArgs(args) {} EventN() {} template<size_t N> void set(typename std::tuple_element<N-1,Tuple>::type value) { std::get<N-1>(mArgs) = value; //I use N-1, to start from '1' } }; The set function above works as I expect it to: auto event = new EventN<String,int>(); event->set<1>("testEvent"); event->set<2>(12); Now,

How to “iterate” over a list of templates at compile time?

我们两清 提交于 2019-12-10 17:55:50
问题 This is the extraction of a follow-up question to this answer. Given the following "loop" technique #pragma once // loop.hpp #include <type_traits> #include <utility> template<std::size_t... indices, class LoopBody> void loop_impl(std::index_sequence<indices...>, LoopBody&& loop_body) { (// C++17's fold expression loop_body(std::integral_constant<std::size_t, indices>{}), ... ); } template<std::size_t N, class LoopBody> void loop(std::integral_constant<std::size_t, N>, LoopBody&& loop_body) {

Iterating a parameter pack with variadic templates in c++

孤街浪徒 提交于 2019-12-10 17:40:46
问题 As part of a logging library, I would like to be able to iterate a parameter pack, writing each value to a stream. My first attempt doesn't compile however. The first error is "error C2144: syntax error : 'int' should be preceded by '}'". #include <sstream> #include <ostream> #include <iomanip> #include <fstream> template <typename ...Args> std::ostream & Write(std::ostream & o, std::initializer_list<Args...> list) { size_t size = list.size(); if(list.size() > 0) { for(size_t i = 0; i < (size

Counting function arguments at compile time

断了今生、忘了曾经 提交于 2019-12-10 16:38:47
问题 I'm trying to count the number of arguments to a function at compile time (I'm wrapping sprintf up in some templates for compile time checks and type safety). I need to check that the number of arguments matches the number of formatting placeholders at compile time. A first pass at this is pretty simple: template <typename... Args> constexpr u32 CountArgs(Args&&... args) { return sizeof...(args); } constexpr u32 CountFormatSpecifiers(c8* format); template <typename... Args> c8* String

Parameter pack expansion fails

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:58:20
问题 Consider the following simplified C++ code: template <typename ... TEventArgs> struct Event { // ... }; template <typename T> struct Parameter { using Type = T; // ... }; template <typename ... Parameters> struct Command { Event<typename Parameters::Type...> Invoked; }; int main() { Command<Parameter<int>, Parameter<float>> c; } The Visual Studio C++ compiler (November 2013 CTP, Visual Studio 2013 Update 1) produces the following error: source.cpp(17): error C3546: '...' : there are no

Why can't compilers expand arguments of a variadic template via comma operator?

China☆狼群 提交于 2019-12-10 15:31:56
问题 I know that we can't use variadic expansions as if it is a chain of comma operators. In that question the sample is like this: template<typename... Args> inline void increment_all(Args&... args) { ++args...; } It might be ambiguous either to increment or expand first so parentheses won't hurt: template<typename... Args> inline void increment_all(Args&... args) { (++args)...; } or something like this: template<typename... Args> void cout_all(Args&&... args) { (std::cout << std::forward<Args>