variadic-templates

Add all parameters with parameter pack expansion [duplicate]

杀马特。学长 韩版系。学妹 提交于 2021-02-06 04:43:35
问题 This question already has answers here : How to call a function on all variadic template args? (3 answers) Closed 6 years ago . Consider I have a variadic template with int... parameters. For example a function like this: template<int... t> int add(){ return t... + ??? } All the method should do is adding all the parameters. It can be easily achieved using recursive variadic templates. However, is it also possible expressing this (or something similar like using other binary operators to

Add all parameters with parameter pack expansion [duplicate]

匆匆过客 提交于 2021-02-06 04:43:00
问题 This question already has answers here : How to call a function on all variadic template args? (3 answers) Closed 6 years ago . Consider I have a variadic template with int... parameters. For example a function like this: template<int... t> int add(){ return t... + ??? } All the method should do is adding all the parameters. It can be easily achieved using recursive variadic templates. However, is it also possible expressing this (or something similar like using other binary operators to

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

风流意气都作罢 提交于 2021-02-05 19:58:17
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over “&&”/“||”?

一笑奈何 提交于 2021-02-05 19:56:48
问题 Is there any specific cases you cannot correctly do with std::conjunction / std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over && / || ? Example: // func is enabled if all Ts... have the same type template<typename T, typename... Ts> std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> > func(T, Ts...) { // TODO something to show } vs // func is enabled if all Ts... have the same type template<typename T,

deduction guides for std::array

ぃ、小莉子 提交于 2021-02-05 06:16:45
问题 I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array works. Regarding the definition of the standard the following is the declaration template <class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>; For example if in main a array created as std::array a{42,45,77} How the deduction takes place? Thank you 回答1: How the deduction takes place? It's simple. Calling std::array a{42,45,77} match array(T, U...) with T = decltype(42

Call void function for each template type in a variadic templated function?

你离开我真会死。 提交于 2021-02-04 20:05:29
问题 My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name function for any types, including multiple types at once (variadic): template <typename T> void print_type_name(void) { std::cout << typeid(T).name() << std::endl; } This works fine for things like this: print_type_name<int>(); print_type_name<std::string>(); print_type_name<std::vector<std:

Call void function for each template type in a variadic templated function?

南楼画角 提交于 2021-02-04 20:05:12
问题 My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name function for any types, including multiple types at once (variadic): template <typename T> void print_type_name(void) { std::cout << typeid(T).name() << std::endl; } This works fine for things like this: print_type_name<int>(); print_type_name<std::string>(); print_type_name<std::vector<std:

Call void function for each template type in a variadic templated function?

被刻印的时光 ゝ 提交于 2021-02-04 20:05:02
问题 My goal is to write a simple generic function for registering converters for arbitrary C++ types. For simplicity I'll just print C++ type names. I'd like to be able to call my generic print_type_name function for any types, including multiple types at once (variadic): template <typename T> void print_type_name(void) { std::cout << typeid(T).name() << std::endl; } This works fine for things like this: print_type_name<int>(); print_type_name<std::string>(); print_type_name<std::vector<std:

Unpacking variadic tuples in c++17

戏子无情 提交于 2021-02-04 17:27:50
问题 Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template <typename ...I> class MultiIterator { public: MultiIterator(I const& ...i) : i(i...) {} MultiIterator& operator ++() { increment(std::index_sequence_for<I...>{}); return *this; } private: template <std::size_t ...C> void increment(std::index_sequence<C...>) { std::ignore = std::make_tuple(++std::get<C>(i)...); } std::tuple<I...> i; }

Extract template template parameter and variadic template parameter from class template

别说谁变了你拦得住时间么 提交于 2021-01-29 18:33:36
问题 Given the following class template: template <template <typename... Args> class Container, typename... Args> struct container_type_holder {}; I would like to extract its template template parameter and its variadic parameter to reuse in another context. Example: using c1 = container_type_holder<std::map, std::string, int>; using c2 = container_type_holder<tt_parameter<c1>, vt_parameter<c1>>; Where tt_parameter<c1> is some magic trick to extract the template template parameter from c1 and vt