template-meta-programming Questions

4

c++17 provides if constexpr, in which: the value of condition must be a contextually converted constant expression of type bool. If the value is true, then statement-false is discarded (if prese...

4

Solved

Consider the following program: #include <tuple> #include <vector> #include <iostream> #include <type_traits> template <class T> struct ordered {}; template <cla...
Ahem asked 10/2, 2018 at 18:9

10

Solved

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 ...
Rubbery asked 23/2, 2012 at 4:51

4

Is there any way to map enum values to types in C++, including C++11. I have the following enum type: enum ATTRIBUTE{AGE=0, MENOPAUSE, TUMOR_SIZE, INV_NODES, NODE_CAPS, DEG_MALIG, BREAST, BREAST_...

2

So I'm trying to make a type trait that says whether two "outer" class types are the same. ie. std::vector<int> is the same as std::vector<double>, I don't care about any inner argumen...

1

I've encountered a really weird problem with clang. When I compile my code with C++20 using gcc-11 everything is fine. The problem appears when I try to compile it with C++20 and clang-14 (using cl...

1

Solved

I want to define a concept in C++ (<= C++20) to check if a type matches any of the types define in a type-list struct. The following is my attempt so far: template<typename... Types> struc...

2

Solved

I have tried to create a compile-time simple Key-Value map in C++. I'm compiling with /std:c++11. (Using IAR compiler for embedded code and only cpp++11 is supported at the moment) I've learnt a l...

3

Is there any compile-time library (template metaprogramming) for arbitrary-precision arithmetic in C++? I need this to help with fixed-point arithmetic and binary scaling in my program for A...

3

Solved

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 ju...

4

Solved

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 |=...

1

Solved

I have the following small C++ code sample, but I can't figure out why the compiler works this way, although I have spent quite a lot of time studying cppreference. I would appreciate any explanati...

5

Solved

Suppose I have two classes: template <typename X, typename Y> class Functor {}; template <typename Start, typename End, typename ...Functors> class Template {}; Template has the const...

4

Solved

I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. However, I couldn't find on...
Polyclitus asked 17/4, 2014 at 12:53

2

I wanted to check if there's an intuitive and easy way to access struct fields by name in modern C++. I am aware that similar questions have been asked and answered, and C++ reflection is a well in...
Loiseloiter asked 18/8, 2020 at 10:58

4

Solved

I would like to create a compile-type function that, given any callable object f (function, lambda expression, function object, ...) and a type T, evaluates to true, if f can be called with an argu...
Enisle asked 5/4, 2014 at 14:29

4

Solved

While playing around with compile-time string (variadic lists of char) manipulation, I needed to implement a way of checking if a compile-time string contained another (smaller) compile-time string...
Belletrist asked 10/2, 2015 at 13:32

1

Solved

I have this class template <typename ValueType, std::size_t Size> struct ArrayPrimitive { constexpr ArrayPrimitive(const ValueType (&array)[Size]) { std::copy(array, array + Size, data_...

3

Solved

I am in search of a ::std::function usable in constexpr. Use case: I have a function which takes a function pointer as an argument, and a second which passes a lambda to the first function. Both ar...

24

Solved

How can I iterate over a tuple (using C++11)? I tried the following: for(int i=0; i<std::tuple_size<T...>::value; ++i) std::get<i>(my_tuple).do_sth(); but this doesn't work: ...
Stefanstefanac asked 29/7, 2009 at 5:57

1

The title should clarify what my confusion is about. I'd like to use this question to get a comprehensive answer which helps me understand how the comma operator works with decltype within SFINAE c...

2

Solved

I am wondering whether there would be a trick to simplify the writing of a trait to return whether a type is a noexcept function. Currently my implementation is the following, and it just lists all...
Kumar asked 16/6, 2020 at 14:42

6

Solved

In c++, how can I implement a function with an int template argument indicating the tuple length and produce a std::tuple with that length? E.g. func<2>() returns std::tuple<int, int&gt...
Caespitose asked 11/8, 2016 at 0:8

4

Solved

I'm not sure that this is possible, but say I have: using my_variant = std::variant<Class1, Class2, Class3>; Now at some point, I create a Class4 and would like to extend my_variant2 to inc...
Baikal asked 18/9, 2018 at 20:2

1

Solved

I have a class with several template methods, i.e.: class MessageEndpoint { public: using SupportedMessages = boost::mp11::mp_list<messaging::MessageType1, messaging::MessageType2, messag...

© 2022 - 2024 — McMap. All rights reserved.