variadic-templates

Matching variadic non-type templates

烂漫一生 提交于 2019-12-05 09:45:34
问题 Let's say I have two structs, Foo and Bar : template<int...> struct Foo{}; template<unsigned long...> struct Bar{}; I want to create a type trait (call it match_class ) that returns true if I pass two Foo<...> types or two Bar<...> types, but false if I try to mix them: int main() { using f1 = Foo<1, 2, 3>; using f2 = Foo<1>; using b1 = Bar<1, 2, 3>; using b2 = Bar<1>; static_assert(match_class<f1, f2>::value, "Fail"); static_assert(match_class<b1, b2>::value, "Fail"); static_assert(!match

Questions on lambda overloads, type conversions and perfect forwarding

喜你入骨 提交于 2019-12-05 09:35:18
This is a question on lambda overload sets and perfect forwarding and somewhat of a followup to a comment . For more context of how this is used see another related question . I have some questions on the below code snippet. Q1: For lambda overloads, I was using overload(Fs...) -> overload<Fs...> from this post , but then in this answer I saw overload(Fs&&...) -> overload<std::decay_t<Fs>...> . In what situations is this difference relevant? Q2: Why would you want to define the identity function below with return decltype(x)(x) and not just return x ? Q3: Can we consider foo(convert(std:

How to get the index of a type in a variadic type pack?

好久不见. 提交于 2019-12-05 09:06:22
For example template<typename T, typename... Ts> struct Index { enum {value = ???} }; and assume T is one of Ts and Ts has different types, like Index<int, int, double>::value is 0 Index<double, int, double>::value is 1 #include <type_traits> #include <cstddef> template <typename T, typename... Ts> struct Index; template <typename T, typename... Ts> struct Index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {}; template <typename T, typename U, typename... Ts> struct Index<T, U, Ts...> : std::integral_constant<std::size_t, 1 + Index<T, Ts...>::value> {}; You might like to add a C++14

Enable template only for specific templated class

自作多情 提交于 2019-12-05 06:38:28
This question is inspired from my previous question No template parameter deduction of parameter pack . Consider following code example: #include <memory> #include <string> template<typename... FArgs> class Callback { public: class Handle{}; }; class BaseCallbackHandle { }; using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>; template<typename H> TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle) { return {}; } int main() { Callback<int>::Handle h; std::string s; makeTypeErasedCallbackHandle(h); //should compile fine makeTypeErasedCallbackHandle(s); //should

How to swap two parameters of a variadic template at compile time?

烈酒焚心 提交于 2019-12-05 06:04:40
I'm trying to swap two parameters of a variadic template at compile time : template<int...Numbers>struct sequence{}; template<size_t first,size_t second> struct Swap_Pair { const static size_t First = first; const static size_t Second = second; }; template <int...Numbers,class swap_pair> struct Swap_Data { static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ? }; The use case should be : sequence<1, 2, 3, 4, 5, 6> array; auto result = Swap_Data < array, Swap_Pair<2, 5> > ::data_; //result is now std::array which contains 1 2 6 4 5 3 I

How to create a variadic template string formatter

懵懂的女人 提交于 2019-12-05 05:18:52
We need to format strings all the time. It would be so nice to be able to say: std::string formattedStr = format("%s_%06d.dat", "myfile", 18); // myfile_000018.dat Is there a C++ way of doing this? Some alternatives I considered: snprintf : uses raw char buffers. Not nice in modern C++ code. std::stringstream : does not support format pattern strings, instead you must push clumsy iomanip objects into the stream. boost::format : uses an ad-hoc operator overload of % to specify the arguments. Ugly. Isn't there a better way with variadic templates now that we have C++11? It can certainly be

variadic template function to concatenate std::vector containers

安稳与你 提交于 2019-12-05 04:57:57
问题 While learning about template parameter packs, I'm trying to write a clever, simple function to efficiently append two or more std::vector containers together. Below are two initial solutions. Version 1 is elegant but buggy, as it relies on side-effects during the expansion of the parameter pack, and the order of evaluation is undefined. Version 2 works, but relies on a helper function that requires two cases. Yuck. Can you see if you can come up with a simpler solution? (For efficiency, the

Unpacking a typelist

夙愿已清 提交于 2019-12-05 04:45:42
Lets say I have a function that takes just a type template parameter, I cannot change it's definition/implementation. template < typename T > void do_it(); Now I have a typelist defined a usual way, can't change it either: template< typename ...Ts > struct typelist; I want to implement a function that takes a typelist, and runs do_it() on every type: template< typename List > void do_them(); The only solution I found up 'till now is: template< typename T > void do_them_impl() { do_it<T>(); } template< typename T, typename Ts...> void do_them_impl() { do_it<T>(); do_them_impl<Ts...>(); }

Could not convert from brace-enclosed initializer list to std tuple

醉酒当歌 提交于 2019-12-05 04:35:57
As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support is limited since I get the following error (GCC 7.2.0): main.cpp: In function 'int main(int, const char*

Dependent non-type parameter packs: what does the standard say?

☆樱花仙子☆ 提交于 2019-12-05 04:10:33
I think the following code is well-formed: template< typename T > using IsSigned = std::enable_if_t< std::is_signed_v< T > >; template< typename T, IsSigned< T >... > T myAbs( T val ); Others say that it is ill-formed, because §17.7 (8.3) of the C++17 standard: Knowing which names are type names allows the syntax of every template to be checked. The program is ill-formed, no diagnostic required, if: (...) every valid specialization of a variadic template requires an empty template parameter pack , or (...) In my opinion IsSigned< T >... is a dependent template parameter, therefore it can not