variadic-templates

C++11 indexing template parameter packs at runtime in order to access Nth type

孤街醉人 提交于 2019-12-08 17:11:41
问题 From this SO topic (and this blog post), I know how to access Nth type in a template parameter pack. For instance, one of the answers to the abovementioned SO question suggests this: template<int N, typename... Ts> using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type; using ThirdType = NthTypeOf<2, Ts...>; However, these methods work only in compile-time. Trying to do something such as: int argumentNumber = 2; using ItsType = NthTypeOf<argumentNumber, Arguments...>; would

How to overload variadic templates when they're not the last argument

僤鯓⒐⒋嵵緔 提交于 2019-12-08 16:25:34
问题 Basically the problem can be summed up with this example: template <typename ...Us> void foo(Us...) { std::cout << "A\n"; } template <typename ...Us> void foo(Us..., int) { std::cout << "B\n"; } int main(){ foo(1,2,3); } This calls the first foo (prints A ). How can I get it to call the second foo ? If this used a non-variadic template, or if the "int" was the first argument, then the overload rules would call the right function. That is, a specific type ( int ) is a better match than a

clang vs gcc: variadic lambda captures

杀马特。学长 韩版系。学妹 提交于 2019-12-08 15:21:00
问题 I am trying to capture a variadic lambda argument inside a inner lambda and use it there. As an example, consider this code: int main () { auto first = [&] (auto&&... one) { auto second = [&] (auto&&... two) { return ((one * two) + ...); }; return second(one...); }; return first(5); } This works with gcc9 but fails with clang8 (https://godbolt.org/z/i2K9cK). A way to make the code compile is to explicitly capture [&one...] , but i was wondering whether this is a bug in clang. Also interesting

User defined literals for variadic char template

岁酱吖の 提交于 2019-12-08 13:35:48
Recently, in the gcc-trunk sources was implemented the "user defined literals". Tell me please, do I understand correctly that I can`t define a "user defined literals" for variadic char template? template<char... chars> int operator"" _call() { return sizeof...(chars); } ... std::cout << "method"_call; Up. I don`t understand why this expression is allowed: template<char... chars> int operator"" _call() { return sizeof...(chars); } ... std::cout << 12345566_call; and this one is disallowed: template<char... chars> int operator"" _call() { return sizeof...(chars); } ... std::cout << method_call;

Difficulties using Variadic Templates

落花浮王杯 提交于 2019-12-08 10:05:08
问题 I'm writing a networking-related class. My application receives network messages of the form [uint8_t message id, uint8_t/uint16_t/uint32_t data ...] My class allows its user to register a callback for a specific message id. Since there are variety of different messages with different number of different data entries (data entries are restricted to uint8_t, uint16_t and uint32_t), I decided to use C++11's variadic templates to lessen the burden of repeated code. Here is my pseudo-code of what

Variadic Template, no matching function call

北城余情 提交于 2019-12-08 07:44:57
问题 I'm writing an implementation of zip , but I've ran into a bit of a problem. Here's a minimal test case: #include <iostream> #include <deque> #include <tuple> #include <string> #include <limits> template <template <typename...> class Container, typename... Types> Container<std::tuple<Types...>> zip(Container<Types> const&... args) { unsigned len = commonLength(args...); Container<std::tuple<Types...>> res; std::tuple<Types...> item; for (unsigned i=0; i<len; i++) { item = getTupleFrom(i, args

Converting a variadic macro to a variadic template function?

喜夏-厌秋 提交于 2019-12-08 06:54:53
问题 Given a variadic macro of the form: #define MY_CALL_RETURN_F(FType, FId, ...) \ if(/*prelude omitted*/) { \ FType f = (FType)GetFuncFomId(FId); \ if(f) { \ return f(__VA_ARGS__); \ } else { \ throw invalid_function_id(FId); \ } \ } \ /**/ -- how can this be rewritten to a variadic function template? template<typename FType, typename ...Args> /*return type?*/ tmpl_call_return_f(MyFunId const& FId, /*what goes here?*/) { ... FType f = (FType)GetFuncFomId(FId); return f(/*what goes here?*/); ...

parameter pack expansion not working in lambda

吃可爱长大的小学妹 提交于 2019-12-08 06:31:27
I am learning variadic templates by doing some exercises and I am stuck when it comes to parameter pack expansion in lambda So, my idea is to write a timer class whose payload will be callable, But I get a compilation error when I try to expand the parameter pack inside a lambda function.. gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) template<typename F, typename... Args> struct timer { timer(const std::chrono::milliseconds milliseconds, F call, Args&&... args) { m_timer = std::make_shared<boost::asio::steady_timer>( timer_manager::instance().get_io_service(), std::chrono::steady_clock:

How to use CRTP with variadic templates?

拜拜、爱过 提交于 2019-12-08 06:19:16
问题 Let's suppose originally I have the following design using CRTP: template<class Outputter> class Generator { protected: vector<int> v; private: void work(ostream& out) { // perform first part of some complex operations on v out << *static_cast<Outputter *>(this); // perform second part of some complex operations on v out << *static_cast<Outputter *>(this); // many more .... // perform some final actions } public: Generator(unsigned length): v(length) {} friend ostream& operator<<(ostream& out

Iterating variadic template arguments in reverse order

瘦欲@ 提交于 2019-12-08 05:57:28
问题 The following code is working if I manual reverse the order of the template arguments which is passed to it: template<typename HeadTag, typename... TailTag> struct Mapped_scope_deep : public Mapped_scope_deep<TailTag...> { typedef typename boost::mpl::at<typename Mapped_scope_deep<TailTag...>::type::type_map, HeadTag>::type type; }; template<typename HeadTag> struct Mapped_scope_deep<HeadTag> { typedef typename boost::mpl::at<type_map, HeadTag>::type type; }; Example: // typename Mapped_scope