template-meta-programming Questions

2

I am looking for a c++ template which finds the common parent of a set of given classes. For example class Animal { ... }; class Mammal : public Animal { ... }; class Fish : public Animal { ... }; ...
Wigwam asked 7/1, 2021 at 20:24

4

Solved

I have seen this question which allows one to check for the existence of a member function, but I'm trying to find out whether a class has a member type. In the example below, both evaluate to "fa...
Carhop asked 5/8, 2012 at 4:22

5

I just want to know what is the purpose of std::identity? I could not find anything useful on web. I know how it is implemented : template <typename T> struct identity { T operator()(T x) c...
Shooter asked 20/1, 2017 at 15:34

1

Solved

I'm just playing around with some SFINAE code and I can't get it to work. I'm using Xcode 12.2 with -std=c++17. godbolt reveals that Clang does indeed choose the false_type variant no matter ...
Linhliniment asked 7/12, 2020 at 19:32

3

Solved

I have a class taking several template parameters : template<typename... ELEMENTS> class MyContainer; By definition, MyContainer<A, B, C> is a different type than MyContainer<B, A, ...
Matrass asked 11/11, 2020 at 21:49

1

Solved

The following code attempts to partially specialize a class using a concept and add a method to the specialization, but it is rejected by clang 11.0.0: #include <concepts> template <typen...
Territus asked 11/11, 2020 at 4:43

2

Solved

I am currently learning metaprograming in C++, and I'm trying to see whether an element of a tuple is a pointer. I tried this approach: int a = 3, b = 4; auto tup = std::make_tuple(&a, b); std:...
Oldham asked 11/10, 2020 at 7:22

2

Solved

This question is based on this post. Goal: I would like to know if a class has the member variable x. I would like to receive true regardless whether or not this variable is private, public or prot...
Blindworm asked 30/9, 2020 at 14:17

3

Solved

I am writing unit tests for undergraduate students and want to enforce certain members as public or private. I am aware of methods to actually test private members, e.g., #define private public or ...

3

Solved

It is clear that std::array<type,count> can't store references. However it is possible to write std::tuple<int &, int &, int &, int &, int &> and get what you...
Momentum asked 17/9, 2020 at 9:15

1

Solved

In Functional programming in C++, chapter 11 deals with some basic template meta programming. In this context, the author shows this implementation of remove_reference/remove_reference_t, which are...

2

Solved

Can I specialize forward declared template? For example: template <typename T> class A; template <> class A<char> { char a[1000]; }; int main() { [[maybe_unused]] A<char&gt...
Actuality asked 1/9, 2020 at 8:49

2

Solved

I'm trying to learn some arcane stateful template metaprogramming tricks. (Here's why I want to learn it. Unfortunately this library doesn't work on GCC 8 nor on Clang.) The first obvious thing I ...
Heterosexual asked 30/7, 2018 at 19:59

6

Solved

I'm trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream. I'm missing something because I'm always getting true for a simple empty...
Dextrosinistral asked 31/3, 2014 at 9:39

3

I have a C++ function that takes in an array of uint8_t performs various functions on it that require a uint16_t and emits a smaller uint8_t array. For a uint16_t input I might need a uint32_t arra...

1

I am using an if constexpr to test the presence of a method in a class. If the method is not present, I wish to tell the user the function was ignored and he should implement it but it is not manda...
Amersham asked 10/7, 2020 at 16:29

5

This is a bit of a puzzle rather than a real-world problem, but I've gotten into a situation where I want to be able to write something that behaves exactly like template<int N> struct SortM...
Kunz asked 24/10, 2013 at 7:39

2

Solved

Consider the following code: template <class R, class... Args> using function_type = R(*)(Args...); struct base { template <class R, class... Args> constexpr operator function_type&l...

2

Solved

I'm going to start with how I imagine using the code I'd like to create. It doesn't have to be exactly like this but it's a good example of what I mean by "concise" in the title. In my ca...
Naphthyl asked 18/6, 2020 at 11:20

2

I have some generic code that would like to know when it has been passed a sequence of objects the number of which is known at compile time, as then it can choose an alternative algorithmic strate...
Mazonson asked 5/6, 2016 at 11:36

2

Solved

I need a meta-function that for given complete class type returns its template (e.g. f<foo<bar>>::type or f<foo<baz>>::type results in foo). Or it may return true on f<f...

3

We have this little metaprogramming marvel called std::conditional described here. In the same reference it says that a possible implementation is template<bool B, class T, class F> struct ...
Nonparticipating asked 14/6, 2017 at 17:18

0

It used to be possible, using some C++ compilers, to check whether a templated type has already been instantiated, so that the following program syntax compiles without error: template <typenam...
Middlesworth asked 3/4, 2020 at 14:29

1

Solved

I'm trying to provide a wrapper around std::invoke to do the work of deducing the function type even when the function is overloaded. (I asked a related question yesterday for the variadic and meth...

4

Solved

What's the easiest way to default construct an std::variant from the index of the desired type, when the index is only known at runtime? In other words, I want to write: const auto indx = std::var...
Carranza asked 6/3, 2020 at 12:31

© 2022 - 2024 — McMap. All rights reserved.