variadic-templates

Is there a way to define a variadic number of arguments of the same type?

别说谁变了你拦得住时间么 提交于 2020-06-21 05:29:37
问题 I can't figure out how to implement a function with a variable number of arguments of the same type. I'm writing for a microcontroller with little stack and memory, so I can't use recursion or the STL (the parts with exceptions). Is it possible to make such a function? struct S{ int r1; int r2; }; template<S* s, int... args> fun(int arg1, int arg2); which expands to something like this: for(int arg:args){ s->r1+=7*arg; } example of invocation: S s; const int mode=3, speed=1; fun<&s,1,2,7,4>

Test if std::common_type exists

拟墨画扇 提交于 2020-06-11 03:45:30
问题 I want to write a helper template that checks if a template parameter pack has a common type, i.e., if applying std::common_type to the pack defines a type. Using std::void_t with SFINAE I came up with the following definition: template<typename... Types, typename Enable = void> struct has_common_type : std::false_type { }; template<typename... Types> struct has_common_type<Types..., std::void_t<std::common_type<Types...>::type>> : std::true_type { }; This however does not work, because the

Test if std::common_type exists

耗尽温柔 提交于 2020-06-11 03:41:52
问题 I want to write a helper template that checks if a template parameter pack has a common type, i.e., if applying std::common_type to the pack defines a type. Using std::void_t with SFINAE I came up with the following definition: template<typename... Types, typename Enable = void> struct has_common_type : std::false_type { }; template<typename... Types> struct has_common_type<Types..., std::void_t<std::common_type<Types...>::type>> : std::true_type { }; This however does not work, because the

Test if std::common_type exists

末鹿安然 提交于 2020-06-11 03:41:34
问题 I want to write a helper template that checks if a template parameter pack has a common type, i.e., if applying std::common_type to the pack defines a type. Using std::void_t with SFINAE I came up with the following definition: template<typename... Types, typename Enable = void> struct has_common_type : std::false_type { }; template<typename... Types> struct has_common_type<Types..., std::void_t<std::common_type<Types...>::type>> : std::true_type { }; This however does not work, because the

How to check if every type in a parameter pack is unique? [duplicate]

Deadly 提交于 2020-06-10 11:19:41
问题 This question already has answers here : check variadic templates parameters for uniqueness (4 answers) Closed 3 years ago . For a fixed number of template parameters it is easy, although the number of manually written checks grows quadratically. #include <type_traits> template < typename T1, typename T2, typename T3, typename T4> struct unique_types { static_assert(!std::is_same<T1, T2>::value, "Types must be unique"); static_assert(!std::is_same<T1, T3>::value, "Types must be unique");

How to check if every type in a parameter pack is unique? [duplicate]

风流意气都作罢 提交于 2020-06-10 11:18:07
问题 This question already has answers here : check variadic templates parameters for uniqueness (4 answers) Closed 3 years ago . For a fixed number of template parameters it is easy, although the number of manually written checks grows quadratically. #include <type_traits> template < typename T1, typename T2, typename T3, typename T4> struct unique_types { static_assert(!std::is_same<T1, T2>::value, "Types must be unique"); static_assert(!std::is_same<T1, T3>::value, "Types must be unique");

How do nested templates get resolved in C++?

只愿长相守 提交于 2020-03-23 05:42:22
问题 I recently asked a question about determining whether an iterator points to a complex value at compile time and received an answer that works. The question is here: How can I specialize an algorithm for iterators that point to complex values? And the solution was a set of templates that determine whether one template is a specialization of another: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template

How do nested templates get resolved in C++?

一世执手 提交于 2020-03-23 05:42:08
问题 I recently asked a question about determining whether an iterator points to a complex value at compile time and received an answer that works. The question is here: How can I specialize an algorithm for iterators that point to complex values? And the solution was a set of templates that determine whether one template is a specialization of another: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template

C++ constexpr : Compute a std array at compile time

对着背影说爱祢 提交于 2020-03-17 08:54:01
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

C++ constexpr : Compute a std array at compile time

半城伤御伤魂 提交于 2020-03-17 08:52:04
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep