variadic-templates

Unpacking parameter packs in template aliases

本小妞迷上赌 提交于 2019-12-05 13:15:43
问题 I run into a problem with unpacking variadic templates into a template alias. The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9: template <typename T, typename...> using front_type = T; template <typename... Ts> struct foo { using front = front_type<Ts...>; }; GCC 4.9 complains: test.cc:7:37: error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ...> using front_type = T' using front = front_type<Ts...>; ^ test.cc:1:15:

How to extract a value from a variadic template parameter pack by index?

此生再无相见时 提交于 2019-12-05 12:49:23
I want to write a function magic_get , which can extract a value from a parameter pack by index, for example: int n = 0; n = magic_get<0>(1, 3, 5, 7); assert(1 == n); n = magic_get<1>(1, 3, 5, 7); assert(3 == n); n = magic_get<2>(1, 3, 5, 7); assert(5 == n); n = magic_get<3>(1, 3, 5, 7); assert(7 == n); How to implement magic_get ? template <size_t N, typename... Args> decltype(auto) magic_get(Args&&... as) noexcept { return std::get<N>(std::forward_as_tuple(std::forward<Args>(as)...)); } Change decltype(auto) to auto and add a trailing return type of decltype(/* the whole returned expression

How to fill array with contents of a template parameter pack?

≡放荡痞女 提交于 2019-12-05 11:52:42
I had nested partially specialized template code working with VS 2015 until I discovered that it was not standards-compliant . I want it to be so I twisted my code to overcome the former issue and also that one and have now hit a hard wall. Using variadic templates and partial specialization I would like to fill an array at compile-time given a fixed set of parameters. What I want to achieve also seems similar to this answer but I did not manage to make it work. Consider the following program: #include <cstdlib> template <typename T, std::size_t Size> struct Array; template <typename T, std:

Error with variadiac template: “parameter pack must be expanded”

核能气质少年 提交于 2019-12-05 11:31:36
问题 Here's a variadic template function that I've written: template<class Container, class Value, class... Args> Value& insert(Container& c, Args&&... args) { c.emplace_back(args); return c.back(); } When I use insert like this I'm getting an error: list<int> lst; int& num = insert<list<int>, int, int>(lst, 4); The error complains about this line in the body of insert : c.emplace_back(args); // <= 'args' : parameter pack must be // expanded in this context What does that mean and how can I fix it

Multiple inheritance with variadic templates: how to call function for each base class?

谁都会走 提交于 2019-12-05 11:27:23
I have a diamond inheritance scheme, where the last child should be able to inherit from many different parents. A /|\ / | \ B C ... | | | * * D E Now imagine I have a class D : public B , class E : public B, public C , etc. From D I want to call the same function of all its parents, which I am guaranteed exists due to the inheritance. My thought was that I could wrap this in some variadic template. Currently I have this: template <typename T> class A { public: A(T t) : mT(t) {} virtual ~A() {} virtual void doThings() = 0; protected: T mT; }; template <typename T, typename A = A<T>> class B :

SFINAE not happening with std::underlying_type

你离开我真会死。 提交于 2019-12-05 11:26:54
问题 Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include <array> #include <iostream> #include <vector> #include <cstdint> enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} // std::begin(h) is defined for h of type H template<typename H, typename... T> static typename std::enable_if<std::is_pointer<decltype(std::begin(std::declval<H>()))*>::value>::type foo(const H&, T&&... t) { std::cout << "container\n"; foo(std::forward<T>(t)...);

Specializing a variadic template template parameter on the minimum number of arguments: legal or not?

半世苍凉 提交于 2019-12-05 11:25:36
问题 I have code: #include <cstdio> template<template<typename...> class> struct Foo { enum { n = 77 }; }; template<template<typename, typename...> class C> struct Foo<C> { enum { n = 99 }; }; template<typename...> struct A { }; template<typename, typename...> struct B { }; int main(int, char**) { printf("%d\n", Foo<A>::n); printf("%d\n", Foo<B>::n); } The idea is that template<typename, typename...> class is a subset of template<typename...> class , so it might be possible to specialize on it.

Variadic templates and multiple inheritance in c++11

烂漫一生 提交于 2019-12-05 10:59:01
i'm trying to achieve something like this: I have a templated base class which i want to inherit dynamically template<typename A, typename B> class fooBase { public: fooBase(){}; ~fooBase(){}; }; desired method: (something like this, not really sure how to do it) template <typename... Interfaces> class foo : public Interfaces... { public: foo(); ~foo(); } and my goal is to have the foo class act like this: second method: class foo() : public fooBase<uint8_t, float> , public fooBase<uint16_t, bool> , public fooBase<uint32_t, int> // and the list could go on { foo(); ~foo(); } with the second

Insert/remove type into variadic template list (parameter pack)

依然范特西╮ 提交于 2019-12-05 10:45:14
What is the best way of implementing index-based insertion and deletion of a type in a variadic template type list (parameter pack)? Desired code/behavior: template<typename...> struct List { /* ... */ }; static_assert(is_same < List<int, char, float>::Insert<int, 0>, List<int, int, char, float> >()); static_assert(is_same < List<int, char, float>::Insert<int, 2>, List<int, char, int, float> >()); static_assert(is_same < List<int, char, float>::Remove<0>, List<char, float> >()); static_assert(is_same < List<int, char, float>::Remove<1>, List<int, float> >()); I tried an implementation based on

can't understand variadic templates in c++

假装没事ソ 提交于 2019-12-05 09:54:42
I was reading about variadic templates and I came across this example. The book mentions that to end the recursion process, the function print() is used. I really can't understand its use. Why does the author use this empty print() function? void print () // can't get why this function is used { } template <typename T, typename... Types> void print (const T& firstArg, const Types&... args) { std::cout << firstArg << std::endl; // print first argument print(args...); // call print() for remaining arguments } A variadic expression can capture 0 arguments or more . Take for example the call print