template-meta-programming

Determining the “optimal” common numeric type in a template parameter pack

落花浮王杯 提交于 2019-11-30 19:33:50
What is the best way to determine a common numeric type in a template parameter pack with: the smallest size, no loss of precision, and no risk of overflow/underflow when converting any type in the parameter pack to this "ideal" common type? The variadic template ( best_common_numeric_type ) could be used like so: template<typename... NumericTypes> auto some_numeric_func(const NumericTypes&...) -> typename best_common_numeric_type<NumericTypes...>::type; And have instantiations like the following: [1] best_common_numeric_type<long, unsigned long, float, double, int>::type = double [2] best

Equivalent of std::transform for tuples

六眼飞鱼酱① 提交于 2019-11-30 18:54:16
I want a function that will behave like std::transform for tuples. Basically the functionality to be implemented is template<size_t From, size_t To, class Tuple, class Func> void tuple_transform(Tuple&& source, Tuple&& target, Func f) { // elements <From, To> of `target` ti become `f(si)`, where // si is the corresponding element of `source` }; I believe that to implement this I'll need a compile time integer range struct, a generalization of std::index_sequence and I've implemented it here with cti::range . I also believe that this type of compile time traversal is ideal here : template<class

Ambiguous overload on argument-less variadic templates

吃可爱长大的小学妹 提交于 2019-11-30 18:48:18
Related: Ambiguous overload accessing argument-less template functions with variadic parameters Simple variadic template function can't instantinate Why is this variadic function ambiguous? Consider this pair of variadic templates: template<typename Dummy> bool All(Param& c) { return true; } template<typename Dummy, Func* f, Func* ...rest> bool All(Param& c) { return f(c) && All<Dummy, rest...>(c); } This works and compiles. However, how to write it without the first template parameter? Sounds trivial? Well, that's what I thought. :-) Let's consider some ideas. Idea #1: template<Func* f, Func*

Building a compile time list incrementally in C++

霸气de小男生 提交于 2019-11-30 18:14:42
问题 In C++, is there a way to build a compile time list incrementally, in the following pattern? START_LIST(List) ADD_TO_LIST(List, int) ADD_TO_LIST(List, float) ADD_TO_LIST(List, double) END_LIST(List) The result of this should be equivalent to: using List = Cons<int, Cons<float, Cons<double, Nil>>>; I also have a restriction that the space between the macros needs to be at whatever scope the whole thing is. I'm planning to define things and register them with the list at the same time using a

Equivalent of std::transform for tuples

∥☆過路亽.° 提交于 2019-11-30 16:57:11
问题 I want a function that will behave like std::transform for tuples. Basically the functionality to be implemented is template<size_t From, size_t To, class Tuple, class Func> void tuple_transform(Tuple&& source, Tuple&& target, Func f) { // elements <From, To> of `target` ti become `f(si)`, where // si is the corresponding element of `source` }; I believe that to implement this I'll need a compile time integer range struct, a generalization of std::index_sequence and I've implemented it here

Compile-time recursive function to compute the next power of two of an integer?

喜欢而已 提交于 2019-11-30 15:32:55
问题 On the Bit Twiddling Hacks website the following algorithm is provided to round up an integer to the next power of two: unsigned int v; // compute the next highest power of 2 of 32-bit v v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; I would like to code a metaprogramming function that will compute the same operation: recursively (for compile-time execution) for any kind of integer (it should even work for possible awkward non-standard integers of any size like 15

Counting With Template Metaprogramming?

ⅰ亾dé卋堺 提交于 2019-11-30 13:06:16
I've been trying to think up a creative solution to this problem (on and off) for some time, but I have not as of yet been able to. I recently considered that it might be solvable with template metaprogramming, though I am not sure due to my relative lack of experience with the technique. Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a unique, static class identifier? Thanks in advance! No. This is a problem that comes up in practice quite

Compare two sets of types for equality

你。 提交于 2019-11-30 12:42:07
How can one check if two parameter packs are the same, ignoring their internal order? So far I only have the frame (using std::tuple ), but no functionality. #include <tuple> #include <type_traits> template <typename, typename> struct type_set_eq : std::false_type { }; template <typename ... Types1, typename ... Types2> struct type_set_eq<std::tuple<Types1...>, std::tuple<Types2...>> : std::true_type { // Should only be true_type if the sets of types are equal }; int main() { using t1 = std::tuple<int, double>; using t2 = std::tuple<double, int>; using t3 = std::tuple<int, double, char>;

Optimal way to access std::tuple element in runtime by index

南楼画角 提交于 2019-11-30 12:11:28
问题 I have function at designed to access std::tuple element by index specified in runtime template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &, _Function) {} template<std::size_t _Index = 0, typename _Tuple, typename _Function> inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type for_each(_Tuple &t, _Function f) { f(std::get<_Index>(t)); for

How to make a function that zips two tuples in C++11 (STL)?

狂风中的少年 提交于 2019-11-30 12:03:04
问题 I recently ran across this puzzle, was finally able to struggle out a hacky answer (using index arrays), and wanted to share it (answer below). I am sure there are answers that use template recursion and answers that use boost ; if you're interested, please share other ways to do this. I think having these all in one place may benefit others and be useful for learning some of the cool C++11 template metaprogramming tricks. Problem: Given two tuples of equal length: auto tup1 = std::make_tuple