template-meta-programming Questions
4
Solved
I was thinking that I could test (with C++14) if a class contained a type, I could do this:
#include <type_traits>
struct X {
using some_type = int;
};
struct Y {};
template <typename T...
Marenmarena asked 10/11, 2021 at 17:30
8
Solved
In this answer I define a template based on the type's is_arithmetic property:
template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){
return to_string...
Urolith asked 12/5, 2015 at 11:45
34
Solved
Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class?
Here's a simple example of what I would want to write:
template<class ...
Mesic asked 2/11, 2008 at 20:10
1
Code sample:
class A
{
static constexpr auto GetInt() noexcept { return 6; }
template<int N>
std::enable_if_t< N >= GetInt(), int> func() { return N; }
};
https://godbolt.org/...
Devlen asked 10/7, 2019 at 11:26
2
Solved
I was watching a C++11/14 metaprogramming talk, where some efficient alternatives for common algorithms and tmp patterns are described.
Most of that efficiency gains come from using variadic templ...
Illaffected asked 18/8, 2014 at 22:6
1
Solved
I have a SFINAE template version of is_same for comparing template template arguments.
It generally works as expected on both templates with fixed and variadic number of template parameters:
namesp...
Lunitidal asked 29/9, 2021 at 22:22
3
In my program I have some code which looks something like this: A collection of functions or classes which uniquely implement a common template via template specialization:
constexpr int NUM_SPECIA...
Eluvium asked 18/8, 2021 at 11:53
3
So with SFINAE and c++11, it is possible to implement two different template functions based on whether one of the template parameters can be substituted.
For example
struct Boo{
void saySomethi...
Hiramhirasuna asked 28/3, 2015 at 15:36
3
Solved
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 t...
Berkow asked 5/4, 2018 at 10:21
5
Solved
I'm trying to use std::unique_ptr with a custom deleter to simplify managing the life of handles returned to me from various C APIs. This is nice in theory, but I'm struggling to find an approach w...
Sadfaced asked 21/3, 2014 at 7:56
5
Solved
How can one check if two parameter packs are the same, ignoring their internal order?
So far I only have the frame (using std::tuple), but no functionality.
#include <tuple>
#include <ty...
Feisty asked 27/2, 2017 at 14:40
1
I have already knew that concept is a compile-time predicate which can constrain templates or auto.
I have discovered that the concepts will return the prvalue of type bool, so I could print ...
Durian asked 2/6, 2021 at 11:59
6
Solved
The other day I asked a very similar question about nested vectors, but I've come across another problem that has me stumped. I need to get the innermost type of a nested vector at compile time so ...
Crosscheck asked 27/12, 2019 at 16:37
15
Solved
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&g...
Supersensible asked 12/8, 2011 at 10:25
3
Solved
I'm trying to conditionally add types to a tuple template type based on some compile time condition, as follows:
template <typename T>
class Base { ... }
template <int I>
class Derived...
Hardness asked 19/4, 2021 at 1:45
2
I am looking for an advice on how to implement a DAG in C++ using templates. The main idea is to design a kind of framework where users can bring their own classes (Nodes) to perform some work on t...
Beanfeast asked 1/4, 2021 at 20:54
17
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__ an...
Troopship asked 26/9, 2011 at 22:26
5
Solved
I have this toy example,
template <typename TChild>
struct Base {
template <typename T>
using Foo = typename TChild::template B<T>;
};
struct Child : Base<Child> {
templ...
Droopy asked 17/3, 2021 at 7:21
3
I am writing a simple Entity Component System framework in which I want to use variadic templates to get more flexible interface. For each component I have offset (from the begining of chunk's memo...
Siana asked 16/3, 2021 at 19:8
1
Solved
Is there a possible way to implement sort of helper struct using C++14 which takes as a template argument a Number, return type Ret and input type T and would contain a member type std::function<...
Ahlgren asked 15/3, 2021 at 11:54
2
Solved
If I have
template <typename T> struct A;
template <typename T> struct B;
template <typename T> struct C;
template <typename T> struct D;
what is the most compact way of te...
Bitartrate asked 1/3, 2021 at 12:34
4
Solved
Suppose I have:
template<int... N>
class seq
{
};
template<int... N>
struct uniq{
using type = seq<N...>;
};
I need to make the sequence unique somehow, so that
std::is_same_v&...
Decemvirate asked 23/2, 2021 at 15:4
1
Solved
I am trying to create a templated can_stream struct that inherits from
std::false_type or std::true_type depending on whether operator<< is defined for type T.
#include <iostream>
stru...
Henriettehenriha asked 16/2, 2021 at 20:28
7
Solved
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>
#incl...
Fresher asked 16/7, 2012 at 4:55
1
Solved
I have the following code that can convert a lambda into a C-style function pointer. This works for all lambdas including lambdas with captures.
#include <iostream>
#include <type_traits&g...
Draper asked 4/2, 2021 at 0:47
© 2022 - 2024 — McMap. All rights reserved.