template-meta-programming

Apply the first valid function of a set of N functions

早过忘川 提交于 2019-11-28 10:14:15
This previous answer shows how to apply function based on the validity of a call: here . However, it applies to two functions. I was wondering if the concept could be generalized to N functions using smart template programming tricks, in C++14. The problem is the following: template <std::size_t N, class... X> /* [Return type] */ apply_on_validity(X&&... x) { // Tricks here } // The function would be equivalent to a hypothetical // template <std::size_t N, class... F, class... Args> // [Return type] apply_on_validity(F&&... f, Args&&... args) // where N = sizeof...(F) is the number of

C++ construct that behaves like the __COUNTER__ macro [duplicate]

烈酒焚心 提交于 2019-11-28 09:29:55
This question already has an answer here: Does C++ support compile-time counters? 7 answers I have a set of C++ classes and each one must declare a unique sequential id as a compile-time constant. For that I'm using the __COUNTER__ built-in macro which translates to an integer that is incremented for every occurrence of it. The ids need not to follow a strict order. The only requirement is that they are sequential and start from 0: class A { public: enum { id = __COUNTER__ }; }; class B { public: enum { id = __COUNTER__ }; }; // etcetera ... My question is: Is there a way to achieve the same

details of std::make_index_sequence and std::index_sequence

て烟熏妆下的殇ゞ 提交于 2019-11-28 08:38:42
I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence 's (used for tuple implementation). I see sample code around there, but I really want a dumbed down step by step explanation of how an std::index_sequence is coded and the meta programming principal in question for each stage. Think really dumbed down :) I see sample code around there, but I really want a dumbed down step by step explanation of how an index_sequence is coded and the meta programming principal in

Automatically count the number of instantiated classes in a TMP?

不问归期 提交于 2019-11-28 08:33:33
Given a template metaprogram (TMP), do C++ compilers produce build statistics that count the number of instantiated classes? Or is there any other way to automatically get this number? So for e.g. the obiquitous factorial #include <iostream> template<int N> struct fact { enum { value = N * fact<N-1>::value }; }; template<> struct fact<1> { enum { value = 1 }; }; int main() { const int x = fact<3>::value; std::cout << x << "\n"; return 0; } I would like to get back the number 3 (since fact<3>, fact<2>, and fact<1> are instantiated). This example if of course trivial, but whenever you start

Get argument type of template callable object

自古美人都是妖i 提交于 2019-11-28 08:22:40
Consider the following function: template<class F> void register_handler( F& f ) // any callable object { // find out T - the argument type of f } Here f is some callable object, accepting one argument. It may be a function pointer, an std::function or a result of std::bind . The problem is, how to determine the argument type of f and do some actions based on that type? An easy workaround would be to add the type to template explicitly, like template<class T, class F> // T is the argument type of F void register_handler( F& f ) but this seems an overkill because type F should already contain

Implementing variadic type traits

自古美人都是妖i 提交于 2019-11-28 06:54:45
Intro I'm looking for a pattern to convert C++ type traits into their variadic counterparts . A methodology to approach the problem would be appreciated and generative programming patterns to automate the task would be ideal. Example Take the following : std::is_same<T, U>::value; I want to write a trait that works like so : std::are_same<T1, T2, T3, T4>::value; Current approach It's pretty straightforward to implement the are_same ; Seeking a general solution we can come up with a tool for any variadic trait implementing universal quantification : template<template<class,class> class F,

c++ power of integer, template meta programming

此生再无相见时 提交于 2019-11-28 06:31:15
I want to make a function which returns a power of integer. Please read the fmuecke's solution in power of an integer in c++ . However, I want to generalize his solution to the arbitrary type T. Since c++11 has constexpr, I guess this is possible. Naively, I tried something like, template<class T, int N> inline constexpr T pow(const T x){ return pow<N-1>(x) * x; } template<class T> inline constexpr T pow<T, 1>(const T x){ return x; } template<class T> inline constexpr T pow<T, 0>(const T x){ return 1; } Actually this approach failed since the partial specialization for function template is not

Generating one class member per variadic template argument

我怕爱的太早我们不能终老 提交于 2019-11-28 04:32:11
I have a template class where each template argument stands for one type of value the internal computation can handle. Templates (instead of function overloading) are needed because the values are passed as boost::any and their types are not clear before runtime. To properly cast to the correct types, I would like to have a member list for each variadic argument type, something like this: template<typename ...AcceptedTypes> // e.g. MyClass<T1, T2> class MyClass { std::vector<T1> m_argumentsOfType1; std::vector<T2> m_argumentsOfType2; // ... }; Or alternatively, I'd like to store the template

Automatically pick a variable type big enough to hold a specified number

懵懂的女人 提交于 2019-11-28 03:32:53
Is there any way in C++ define a type that is big enough to hold at most a specific number, presumably using some clever template code. For example I want to be able to write :- Integer<10000>::type dataItem; And have that type resolve to the smallest type that is big enough to hold the specified value? Background: I need to generate some variable defintions using a script from an external data file. I guess I could make the script look at the values and then use uint8_t , uint16_t , uint32_t , etc. depending on the value, but it seems more elegant to build the size into the generated C++ code

How can I make this variadic template code shorter using features from C++14 and C++1z?

情到浓时终转凉″ 提交于 2019-11-28 03:08:23
问题 This is a code snippet that I am going to use in order to check whether the variadic template types are unique: template <typename...> struct is_one_of; template <typename F> struct is_one_of<F> { static constexpr bool value = false; }; template <typename F, typename S, typename... T> struct is_one_of<F, S, T...> { static constexpr bool value = std::is_same<F, S>::value || is_one_of<F, T...>::value; }; template <typename...> struct is_unique; template <> struct is_unique<> { static constexpr