template-meta-programming

Choose function to apply based on the validity of an expression

天大地大妈咪最大 提交于 2019-11-30 11:33:42
The problem is the following, in C++14 : Let's have two functions FV&& valid_f , FI&& invalid_f , and arguments Args&&... args The function apply_on_validity should apply valid_f on args if the expression std::forward<FV>(valid_f)(std::forward<Args>(args)...) is valid Otherwise and if std::forward<FV>(invalid_f)(std::forward<Args>(args)...) is a valid expression, apply_on_validity should apply invalid_f on args Otherwise apply_on_validity should do nothing I guess the code should look like something like this: template <class FV, class FI, class... Args, /* Some template metaprog here */> void

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

感情迁移 提交于 2019-11-30 11:14:00
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 = std::tuple<int, double>; using t2 = std::tuple<double, int>; using t3 = std::tuple<int, double, char>

What are the Differences between C++ Templates and Java/C# Generics and what are the limits? [closed]

南楼画角 提交于 2019-11-30 09:51:54
I read an interesting Article/Thread/Discussion from here and i got following questions: What are the limitations of Java/C# generics? What is possible with C++ Templates that is impossible with Java/C# generics? Edit 1 More recommended questions by Eric Lippert What are some patterns that are possible with C# generics but impossible with C++ templates? What's the difference between C#'s true generic types and Java's type erasure generic types? Eric Lippert First off, you might want to read my 2009 article on this subject . The primary difference to my mind between C++ templates and C#

How to check if template argument is a callable with a given signature

ⅰ亾dé卋堺 提交于 2019-11-30 04:55:18
Basically, what I want to achieve is compile-time verification (with possibly nice error message) that registered callable (either a function, a lambda, a struct with call operator) has correct signature. Example (contents of the static_assert are to be filled): struct A { using Signature = void(int, double); template <typename Callable> void Register(Callable &&callable) { static_assert(/* ... */); callback = callable; } std::function<Signature> callback; }; Most of the answers are focused on basically answering the question: can you call the given function object with values of these types.

Using `void_t` to check if a class has a method with a specific signature

ぐ巨炮叔叔 提交于 2019-11-30 04:08:23
问题 At the moment, I'm using this method to check if a class has a method with a specific signature. After attending Walter E. Brown's metaprogramming CppCon2014 talk, I started wondering if void_t could be used in this particular situation to make the code cleaner and more readable. However I'm having trouble thinking in terms of void_t - so far I understand that void_t can help me determine at compile-time whether or not an expression is valid. Example: template< class, class = void > struct

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

蹲街弑〆低调 提交于 2019-11-30 04:06:24
问题 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

Ambiguous overload on argument-less variadic templates

穿精又带淫゛_ 提交于 2019-11-30 03:02:48
问题 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?

Elegantly define multi-dimensional array in modern C++

亡梦爱人 提交于 2019-11-30 03:02:17
Defining multi-dimensional array using the T[][][] syntax is easy. However, this creates a raw array type which doesn't fit nicely into modern C++. That's why we have std::array since C++11. But the syntax to define a multi-dimensional array using std::array is quite messy. For example, to define a three-dimensional int array, you would have to write std::array<std::array<std::array<int, 5>, 5>, 5> . The syntax doesn't scale at all. I'm asking for a fix for this issue. Maybe, this issue cannot be fixed using existing utility provided by C++. In that case, I'm happy with a custom tool developed

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

て烟熏妆下的殇ゞ 提交于 2019-11-30 02:09:24
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_each<_Index + 1, _Tuple, _Function>(t, f); } namespace detail { namespace at { template < typename

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

好久不见. 提交于 2019-11-30 01:55:22
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(1, 'b', -10); auto tup2 = std::make_tuple(2.5, 2, std::string("even strings?!")); How do you create a