variadic-templates

How can I get the index of a type in a variadic class template?

狂风中的少年 提交于 2019-12-18 17:02:07
问题 I have a variadic Engine template class: template <typename ... Components> class Engine; I'd like to assign a number to each component at compile time which is equivalent to their ordering. This would be returned when making the following call: template <typename Component> int ordinal(); So for example if: Engine<PositionComponent, PhysicsComponent, InputComponent> engine; was declared, the call: engine.ordinal<PhysicsComponent>(); would return 1 and a similar call with InputComponent

How can I get the index of a type in a variadic class template?

吃可爱长大的小学妹 提交于 2019-12-18 17:02:01
问题 I have a variadic Engine template class: template <typename ... Components> class Engine; I'd like to assign a number to each component at compile time which is equivalent to their ordering. This would be returned when making the following call: template <typename Component> int ordinal(); So for example if: Engine<PositionComponent, PhysicsComponent, InputComponent> engine; was declared, the call: engine.ordinal<PhysicsComponent>(); would return 1 and a similar call with InputComponent

Iterating on a tuple… again

╄→гoц情女王★ 提交于 2019-12-18 16:32:11
问题 It's been a while that I've been doing C++ but I'm not familiar with templates. Recently, I tried to write a class that wrap a std::vector<std::tuple<Types...>> . This class must have member functions, and I really need to be able to iterate over the tuple. In fact, if I am able to print every element of a tuple (in the order), I would be able to do everything I need. I found a solution using a cast, but I'm not really confident with it since it is based on a cast that I don't really like

Is it possible to typedef a parameter pack?

淺唱寂寞╮ 提交于 2019-12-18 16:07:09
问题 Is it possible to typedef a parameter pack? For example template<class T, class... Args> struct A { typedef T Type; // We typedef it, then its derived class can use it. // How about for parameter packs? // Option 1: typedef Args Arguments; // Option 2: using Arguments = Args; // Option 3: I can put in a tuple, but how can I untuple it to a pack typedef tuple<Args...> Tuple; }; I want to using the above technique to implement the following template<int... VALUES> struct IntegralSequence { enum

Check if one set of types is a subset of the other

喜夏-厌秋 提交于 2019-12-18 14:16:15
问题 How can one check if one parameter pack (interpreted as a set) is a subset of another one? So far I only have the frame (using std::tuple), but no functionality. #include <tuple> #include <type_traits> template <typename, typename> struct is_subset_of : std::false_type { }; template <typename ... Types1, typename ... Types2> struct is_subset_of<std::tuple<Types1...>, std::tuple<Types2...>> : std::true_type { // Should only be true_type if Types1 is a subset of Types2 }; int main() { using t1

How to perform tuple arithmetic in C++ (c++11/c++17)?

£可爱£侵袭症+ 提交于 2019-12-18 13:33:43
问题 I'm trying to write template functions/operators such as + for doing arithmetic operations between two tuples of the same type. For example, for std::tuple<int,double> t = std::make_tuple(1,2); I'd like to do auto t1 = t + t; The logic is simple: to do the arithmetic element-wise. But I can't figure out how to make this work in c++ template programming (c++11/17). My code below doesn't compile with g++ -std=c++11 tuple_arith.cpp . In particular, I can't figure out the right way of getting the

Define multiple methods with parameters from variadic templates

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 12:35:03
问题 I want to define a base template class in a way so that it takes variadic template arguments and defines a virtual method for each argument, where the parameter is the argument type. E.g. Base<int, bool, string> should give me 3 virtual methods: Foo(int) , Foo(bool) , and Foo(string) . I tried the following: template <typename Param> struct BaseSingle { virtual void Foo(Param) {}; }; template <typename... Params> struct Base : public BaseSingle<Params>... { }; Unfortunately, Foo becomes

Concatenating strings (and numbers) in variadic template function

大城市里の小女人 提交于 2019-12-18 11:22:13
问题 I am attempting to write a function that takes a variety of strings or numbers (that work with std::to_string and concatenate them. I've got it working with just strings, but I am having trouble with specializing depending on input as string or number. My code is called like this: stringer("hello", "world", 2, 15, 0.2014, "goodbye", "world") And here is what I've got: inline std::string stringer(const std::string &string) { return string; } template <typename T, typename... Args> inline std:

How to easily create fully “variadic” functions with C++ 98 standard?

我怕爱的太早我们不能终老 提交于 2019-12-18 09:37:28
问题 The library https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116, uses this awesome trick to do "variadic" templates on C++ 98: inline void printfln(const char* fmt) { format(std::cout, fmt); std::cout << '\n'; } template<TINYFORMAT_ARGTYPES(n)> \ void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ std::cout << '\n'; \ } I am trying to improve it by eliminating the requirement to

Having trouble passing multiple initializer lists to variadic function template

こ雲淡風輕ζ 提交于 2019-12-18 07:39:04
问题 I don't understand the error message when trying to pass a variable number of initializer lists: template<typename... Values> void foo(Values...) { } int main() { foo(1, 2, 3, "hello", 'a'); // OK foo({1}, {2, 3}); // ERROR } The error message complains about too many arguments: prog.cpp: In function ‘int main()’: prog.cpp:9:20: error: too many arguments to function ‘void foo(Values ...) [with Values = {}]’ foo({1}, {2, 3}); ^ prog.cpp:2:6: note: declared here void foo(Values...) ^ However,