variadic-templates

Variadic Template Parameter Packs with Alternating Types

馋奶兔 提交于 2020-01-05 03:32:34
问题 I was wondering if it is possible to capture an alternating parameter pattern using a parameter pack. For example, template<typename T, size_t U, typename... Args> class foo<T, U, Args...> { public: foo() : my_T(nullptr), my_U(U) {} private: T* my_T; size_t my_U; foo<Args...> my_next_foo; } So this doesn't work since Args is a parameter pack of only types. Is there a way to modify this so that the typename T, size_t U pattern can be properly captured in a variadic template? Thanks 回答1: Values

Variadic Template Parameter Packs with Alternating Types

时光毁灭记忆、已成空白 提交于 2020-01-05 03:31:33
问题 I was wondering if it is possible to capture an alternating parameter pattern using a parameter pack. For example, template<typename T, size_t U, typename... Args> class foo<T, U, Args...> { public: foo() : my_T(nullptr), my_U(U) {} private: T* my_T; size_t my_U; foo<Args...> my_next_foo; } So this doesn't work since Args is a parameter pack of only types. Is there a way to modify this so that the typename T, size_t U pattern can be properly captured in a variadic template? Thanks 回答1: Values

C++ object keeps templated function and args as members to call later

匆匆过客 提交于 2020-01-04 15:54:23
问题 I have a class Door that implements a method LockCheck() , and a class Stove with a method BurnerCheck() . I want a class House that takes as a constructor argument either Door::LockCheck or Stove::BurnerCheck along with an unknown set of args for the given function. House would then store the function and its args such that it can call them at some later time. For example, auto stove = Stove(); auto stove_check = stove.BurnerCheck; auto burner_args = std::make_tuple<bool, bool>(true, false);

Why can't I manually provide the template arguments?

為{幸葍}努か 提交于 2020-01-04 01:58:08
问题 I have a variadic template function f . This compiles fine (using g++ -std=c++11 and possibly using c++0x ): #include <tuple> template<int ...> struct seq { }; template <typename ...T, int ...S> void f(std::tuple<T...> arg, seq<S...> s) { // ... do stuff } int main() { f<int>(std::tuple<int>(10), seq<0>()); return 0; } The compiler automatically fills in the int ...S that works. However, I can't seem to manually provide the integer arguments: int main() { f<int, 0>(std::tuple<int>(10), seq<0>

c++03 : Variadic templates using boost

◇◆丶佛笑我妖孽 提交于 2020-01-03 04:45:12
问题 I'm trying to find a workaround to use variadic templates within c++03. What I would like to achieve is - within a templated class - instanciated a boost::tuple attribute, which would be composed of a vector for each single class template parameter. This is how it would look like using c++11 variadic templates: template<typename ...Parameters> class Foo { typedef std::tuple<std::vector<Parameters>...> Vectors_t; Vectors_t _vectorsTuple; } I would like to achieve the same using boost::tuple

Specialization of variadic constructor template of class template

試著忘記壹切 提交于 2020-01-02 05:19:46
问题 Here's a class with variadic constructor and it's specializations for copy and move from a temporary. template<class Obj> class wrapper { protected: Obj _Data; public: wrapper(const wrapper<Obj>& w): _Data(w._Data) {} wrapper(wrapper<Obj>&& w): _Data(std::forward<Obj>(w._Data)) {} template<class ...Args> wrapper(Args&&... args): _Data(std::forward<Args>(args)...) {} inline Obj& operator()() { return _Data; } virtual ~wrapper() {} }; When I use one of specializations like this wrapper<int> w1

Use a templated variadic template parameter as specialized parameter

岁酱吖の 提交于 2020-01-02 05:14:29
问题 My title might be wrong - if so, please do correct me, but at some point its hard for me to keep track of what meta thingy I'm actually trying to achieve ;) I have a class function template like this: template<template<typename...> class MapType> Expression Expression::substitute(MapType<std::string, Expression> const& identifierToExpressionMap) const { return SubstitutionVisitor<MapType>(identifierToExpressionMap).substitute(something); } The important part is the MapType. The idea is to

Split parameter pack in 0 … N-1 and Nth element

馋奶兔 提交于 2020-01-02 04:38:27
问题 I would like to split a parameter pack into the first N - 1 and the Nth parameters without using the typical index_sequence & tuple trick but cannot seem to wrap my head around it, yet I'm pretty sure it should be doable? (getting the last item is easy enough with recursion). The final function to call looks like void Fun( Foo a, Bar b ); and a is acquired from a variadic one in turn: template< class... T > Foo CalcFoo( T... args ); My current implementation: //get the last item of the pack

Why is std::endl generating this cryptic error message?

若如初见. 提交于 2020-01-02 02:26:05
问题 If I try to compile the following code I get the following compiler error (see code.) It compiles without error if std::endl is removed. #include <iostream> #include <sstream> #include <utility> namespace detail { template <class T> void print(std::ostream& stream, const T& item) { stream << item; } template <class Head, class... Tail> void print(std::ostream& stream, const Head& head, Tail&&... tail) { detail::print(stream, head); detail::print(stream, std::forward<Tail>(tail)...); } }

Variadic templates without function parameters

你。 提交于 2020-01-02 02:01:08
问题 Can I use variadic templates without using the template parameters as function parameters? When I use them, it compiles: #include <iostream> using namespace std; template<class First> void print(First first) { cout << 1 << endl; } template<class First, class ... Rest> void print(First first, Rest ...rest) { cout << 1 << endl; print<Rest...>(rest...); } int main() { print<int,int,int>(1,2,3); } But when I don't use them, it doesn't compile and complains about an ambiguity: #include <iostream>