template-meta-programming

Implementing variadic type traits

こ雲淡風輕ζ 提交于 2019-11-27 01:35:57
问题 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

Generate random numbers in C++ at compile time

情到浓时终转凉″ 提交于 2019-11-27 01:31:35
问题 I'm trying to precompute random values using C++11's random library at compile time. I'm mostly following examples. What am I doing wrong here? using namespace std; #include <iostream> #include <vector> #include <random> vector<double> rands; typedef std::mt19937_64 RNG; uint64_t seed_val; RNG rng; void initialize() { rng.seed(seed_val); } constexpr vector<double> generate_random( ) //size_t numbers) { int numbers = 1000; std::uniform_real_distribution<double> zero_one(0.0, 1.0); for

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

谁说我不能喝 提交于 2019-11-27 00:05:52
问题 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

Compile-time constant id

笑着哭i 提交于 2019-11-26 22:44:49
问题 Given the following: template<typename T> class A { public: static const unsigned int ID = ?; }; I want ID to generate a unique compile time ID for every T. I've considered __COUNTER__ and the boost PP library but have been unsuccessful so far. How can I achieve this? Edit: ID has to be usable as the case in a switch statement Edit2: All the answers based on the address of a static method or member are incorrect. Although they do create a unique ID they are not resolved in compile time and

TMP: how to generalize a Cartesian Product of Vectors?

▼魔方 西西 提交于 2019-11-26 20:04:15
There is an excellent C++ solution (actually 2 solutions: a recursive and a non-recursive), to a Cartesian Product of a vector of integer vectors . For purposes of illustration/simplicity, let us just focus on the non-recursive version . My question is, how can one generalize this code with templates to take a std::tuple of homogeneous vectors that looks like this: {{2,5,9},{"foo","bar"}} and generate a homogeneous vector of tuple {{2,"foo"},{2,"bar"},{5,"foo"},{5,"bar"},{9,"foo"},{9,"bar"}} If it makes life any easier, let us assume that the internal vectors in the input are each homogeneous.

Is it possible to develop static for loop in c++?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 19:06:27
问题 Is it possible for something like this to exist? template<int Channel> void deduce_mask(Matrix const &src, int mask[]) { //I hope i could become a constant and the compiler would unroll the loop at compile time for(int i = Channel; i != -1; --i) { //mapper is a helper class which translate two and three dimension into one dimension index //constexpr makes it possible to find out the index at compile time mask[mapper(0, 1, i)] = src(row - 1, col)[i]; mask[mapper(1, 1, i)] = src(row, col)[i];

building and accessing a list of types at compile time

烂漫一生 提交于 2019-11-26 18:59:46
I am trying to achieve the following using c++ template metaprogramming. I wish to build up a list of types and then collect these types together and do further compile-time processing on the list. So for example: foo.h: class Foo { ... }; // INSERT ANY CODE HERE bar.h: class Bar { ... }; // INSERT ANY CODE HERE main.h: #include "foo.h" #include "bar.h" struct list_of_types { typedef /* INSERT ANY CODE HERE */ type; }; I can insert any code into the slots above, so long as list_of_types::type resolves to some representation (e.g. a boost::mpl::vector) of a list containing the types Foo and Bar

How to make generic computations over heterogeneous argument packs of a variadic template function?

孤者浪人 提交于 2019-11-26 18:05:41
PREMISE: After playing around with variadic templates a little bit, I realized that achieving anything which goes slightly beyond the trivial meta-programming tasks soon becomes pretty cumbersome. In particular, I found myself wishing for a way to perform generic operations over an argument pack such as iterate , split , loop in a std::for_each -like fashion, and so on. After watching this lecture by Andrei Alexandrescu from C++ and Beyond 2012 on the desirability of static if into C++ (a construct borrowed from the D Programming Language ) I had the feeling that some sort of static for would

Determine if a type is an STL container at compile time

孤街醉人 提交于 2019-11-26 16:17:38
I would like to write a template that will determine if a type is an stl container at compile time. I've got the following bit of code: struct is_cont{}; struct not_cont{}; template <typename T> struct is_cont { typedef not_cont result_t; }; but I'm not sure how to create the necessary specializations for std::vector<T,Alloc>, deque<T,Alloc>, set<T,Alloc,Comp> etc... First, you define your primary template, which will have a member which is false in the default case: template <typename T> struct is_cont { static const bool value = false; }; Then you will define partial specializations for your

C++ iterate into nested struct field with boost fusion adapt_struct

旧时模样 提交于 2019-11-26 16:09:52
问题 Two stackoverflow answers suggest the approach using fusion adapt_struct to iterate over struct fields. The approach looks nice. However, how do you iterate into a field which itself is a struct? Following the previous answers, I come up with the code below. The problem is at the "#if 0" clause the code does not compile. As an alternative solution I created "decode()" function to take a void pointer to the target argument. That works, but loses the type information at compile time. Is there a