template-meta-programming Questions

2

I am working on some low level code with high level interfaces and felt need for comparisons operator for unit testing for plain old data types(like FILETIME struct) but since C++ doesn't even prov...
Bluebeard asked 3/6, 2018 at 13:41

5

Solved

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 ...
Limoges asked 14/1, 2015 at 11:18

2

Solved

My compiler is gcc 4.9.0. The following code cannot be compiled: template<typename T, T i> struct value {}; template<typename T> struct value<T, 0> {}; // error: type 'T' of te...
Establishmentarian asked 9/7, 2014 at 14:18

7

Solved

I have the following problem. I define a N dimensional vector as so #include <vector> #include <utility> #include <string> template <int N, typename T> struct NVector{ t...
Puga asked 29/1, 2020 at 9:59

4

I'm trying to understand what metaprogramming is general and what it is in C++ in particular. If I search for c++ metaprogramming I do get tutorials of template metaprogramming (TMP), but no explan...

1

I want to implement atoi() function at compile time (in C++ language, by using C++11 or C++14 standard). So it should be able to parse text enclosed in double quotes as number, or repor an error. M...
Pyroconductivity asked 30/12, 2019 at 18:26

1

Solved

std::transform provides overloads which take either a unary (one argument) or binary (two argument) callable operation (typically a lambda). I would like to pass my desired callable as an argument...
Fenella asked 30/12, 2019 at 16:54

3

Solved

I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would li...
Jeffreys asked 26/12, 2019 at 16:0

2

Solved

Since concepts are defined as compile-time predicates, is it also possible to actually reuse these predicates for compile-time algorithms? For example would it be possible to check whether all type...
Comparison asked 15/11, 2019 at 11:11

4

Solved

Can somebody explain to me, why the first call using the template function is falling into an infinite loop, while the second compile-time function runs correctly? #include <iostream> using n...
Mull asked 25/11, 2019 at 15:4

3

Solved

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 co...
Ginn asked 14/10, 2014 at 16:58

1

Solved

This compiles: class A { public: template <int, int> class B; }; template <int y, int z = y> class A::B { }; int main() {} This doesn’t: template <int x> class A { public: ...
Antionetteantioxidant asked 8/10, 2019 at 19:24

1

Solved

I am just trying my hands on SFINAE using std::enable_if in C++. I thought i understood the theory part until i couldn't get the following piece of code to compile. Adding to this confusion is the ...
Congo asked 16/9, 2019 at 21:39

3

Solved

In modern C++, does the standard library provide a type list template? int main() { using int_types = type_list<int,long,short,char>; std::cout << length<int_types>::value &lt...
Guitar asked 16/6, 2019 at 11:17

2

Solved

I have some variant using V = std::variant<A, B, C> and a function with the prototype V parse(const json&). The function should try to parse all the types (e.g. A, B, then C) till the fir...

5

Solved

I have a problem where I need to map an integer at compile time to another integer. Basically, I need the compile-time equivalent of std::map<int,int>. If a key is not found in the map, I'd l...
Cocktail asked 10/5, 2013 at 21:6

2

Solved

#include <type_traits> class Base { public: virtual bool f() { return true; } }; template<typename T> class Derived : public Base { std::enable_if_t< std::is_copy_constructible...

3

Solved

Is there a way to get the number of fields of a class? struct Base { char a; int b; }; struct Derived : Base { std::string c; }; static_assert(num_fields<Base>::value == 2); static_asse...
Maugham asked 22/8, 2017 at 14:27

4

Solved

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 unrol...
Abisia asked 11/12, 2012 at 9:4

2

Solved

For implementing a conditional type I highly enjoy std::conditional_t as it keeps the code short and very readable: template<std::size_t N> using bit_type = std::conditional_t<N == std::...

2

Solved

Since C++20 concepts aren't standardized yet, I'm using static_assert as a makeshift concept check, to provide helpful error messages if a type requirement isn't met. In this particular case, I hav...
Beadledom asked 29/6, 2019 at 5:38

2

Solved

I have Conditional template template<bool C, typename ...> struct Conditional { }; template<typename C1, typename C2> struct Conditional<true, C1, C2> { typedef C1 value; }; ...
Salinger asked 27/6, 2019 at 8:49

2

Solved

I have a template Conditional which returns value according to the boolean expression: template<bool C, typename C1, typename C2> struct Conditional { }; template<typename C1, typename C...
Crashland asked 25/6, 2019 at 10:29

1

Solved

I have a need for a constexpr function that is very similar to std::tuple_cat, but instead of merging all elements regardless of what they are into one tuple, I need it to add a given element only ...
Sapodilla asked 14/6, 2019 at 5:12

2

The following excerpt compiles in Clang-libstdc++ or Clang-libc++, GCC, many of their versions and all three versions of the language since 11 (14 & 17): #include <type_traits> st...

© 2022 - 2024 — McMap. All rights reserved.