2,426 questions
0
votes
1
answer
199
views
Typename packing is failed in C++
Here is code:
aml::type_list<int,int,char,std::string> example;
std::tuple_element_t<2,std::tuple<decltype(example)>> a = 'b';
But when i try to run code it says, static assertion ...
0
votes
1
answer
137
views
Why template instantiation requires specialation in the case where it is already done for all cases?
In c++ when i try to write a code that returns reverse of a type list what i wrote( a user defined template class):
template <typename ...V,typename ...D>
constexpr decltype(auto) merge(...
0
votes
2
answers
228
views
C++ check if const qualified variables are the same object at compile-time
I am working on a project that uses some compile-time logic based on whether two arguments are the same object (i.e. same memory address). The following is a simplified example that compiles and runs ...
1
vote
1
answer
143
views
What is explanation/fix for compiler's error when sorting types of tuple using decltype, lambdas and std::array?
I needed to sort types in a tuple by alignment, and as a result I just want to get a new tuple type.
Wrote some implementation. But in one case (when using consteval auto sort_tuple(auto tuple)) gcc ...
2
votes
2
answers
136
views
Deduce maximum number of function arguments (fails with concepts)
I am writing a function probeMaxArgs that deduces the maximum number of arguments in a function/callable object. For simplicity I assume, that the object don't have any overloads of operator() (...
4
votes
0
answers
254
views
Compile-Time Type Registry in C++
I need a way to set up a compile-time "registry" that maps registered types to incrementing int IDs and back again.
I would prefer it to be implemented such that, when used, the library to ...
0
votes
1
answer
89
views
Compile‑time conversion of a JSON literal into a JsonString<char…> type in C++20 – overall design patterns? [closed]
I’m exploring ways in C++20 to represent a JSON text literal entirely in the type system, so that I can then write a metafunction like
template<typename Json, char... Key> struct HasKey;
...
3
votes
0
answers
167
views
Why does C++26 still not allow passing a function template as a template parameter?
In C++26, the following code is legal (see p2841r7 and Herb Sutter's article):
template<typename<typename> concept C, template<typename> auto vt, typename T>
requires C<T>
auto ...
3
votes
2
answers
250
views
C++ template metaprogramming - selecting a range of arguments from a parameter pack
I am making a compile time ECS system and have created an archetype class to store components:
template<typename... Components>
class Archetype;
template<typename T>
struct ...
5
votes
1
answer
210
views
Are structured binding packs allowed in expansion statements outside of template?
Recently, expansion statements have been accepted to C++26 draft. Which means, this gives us another way of iterating through members of destructurable types such as "Point" defined below.
...
9
votes
3
answers
955
views
How to constrain a C++ template template argument to be a child of a templated type?
In the snippet below, base_type_t has 2 template arguments; T1 and k1. The templates foo_t, bar_t, and baz_t derive from this base, using the derived template's parameter for T1, but providing a ...
3
votes
2
answers
99
views
Cannot detect protected base class method with SFINAE
I attempted the following approach using c++ SFINAE and std::declval to find if there exists the proper method in a base class, that can be called from the derived class. The derived class gets the ...
2
votes
1
answer
75
views
variadic-value-template-template with value-types given in parent template is accepted by clang but rejected by gcc
I'm building trait-types (similar to std::type_identity) to represent template-templates of the different flavors. Mixed-type variadic value templates are a special case, and I can only think of doing ...
1
vote
1
answer
129
views
std::conditional_t evaluate false result type when condition is true
I have currently this:
template<TagType TTag, IsTag ...Tags>
struct RequiredTagList
{
static constexpr const TagType index = TTag;
};
// -----------------------------------------------------...
2
votes
1
answer
125
views
declare return_void / return_value conditionally
I've tried std::enable_if and requires:
template <typename Ret>
struct promise_type {
auto get_return_object() {/*...*/}
auto initial_suspend() {/*...*/}
auto return_void()
-> std:...
7
votes
0
answers
121
views
Nested expansion of packs
I've been clarifying some of my understanding of C++ meta-programming. So looking at a function like (my code)
using namespace std;
template<class... As, class F>
constexpr auto
for_each(tuple&...
2
votes
1
answer
109
views
In C++ why can't I deduce a parameter pack for the initial arguments of a function type?
Let's say I have I have some container template type Container that accepts object types, and some std::function-like template FunctionWrapper that accepts function types. Further, I have a struct S ...
4
votes
3
answers
170
views
std::views like operation on parameters pack
I need sometimes to filter some types from a given parameter pack and get the result as a std::tuple. For instance, it can be a filter based on the index of the types in the pack.
As an example, here ...
1
vote
1
answer
81
views
Is there a way to gather explicit specializations of a templated function into a container at compile time or startup?
I am trying to setup a system where I or a user can implement a specialization to a templated function and have it be put into a static collection of function pointers defined in another file, or even ...
2
votes
0
answers
67
views
Can user-defined deduction guides be used to deduce non-deduced context because of nested-name-specifier?
I know that the following code(down bellow or on godbolt) won't be possible as is, because the nested-name-specifier creates a non-deduced context. But I have hopes that with a user-defined deduction ...
4
votes
1
answer
135
views
How to specialize a method template using concepts?
My goal is simple: I want to specialize a method template for multiple types at once.
I've googled around for possible ways to accomplish this task and found that concepts can be used for that.
I've ...
1
vote
1
answer
106
views
Is there a way to have "partially recursive" function constraints in C++?
I'm writing a policy class, TrivialSerializer, that'll slot into a function later in C++. This policy needs to serialize objects whose types may be specified to certain extent by other policies, and ...
4
votes
1
answer
103
views
C++ Type traits dependent upon declaration ordering and varying compiler behavior
I found a strange behavior with a custom type trait I was writing and further found that different compilers are having different behavior with the trait. The goal is simple, I want to detect if a ...
1
vote
0
answers
109
views
Pointer of an object which has static storage as template non-type parameter, Clang and GCC agrees, MSVC doesn't
I have been working on a pet project on C++ where I am trying to parse a json at compile-time (json is stored in a fixed size string, so it's known at compile time). Given that this is not the easiest ...
0
votes
2
answers
135
views
Minimal approach to deduce lots of template parameters
I frequently encounter a kind of problem illustrated in the following minimal hypothetical example.
I have a library function like this:
/**
* `shoes` can take values {0, 1, 2}
* `isNew` can take ...
1
vote
2
answers
94
views
Template function member as template parameter
I want to call different template member functions with the same sequence of input types, in order to reduce boilerplate code, I tried to implement a dispatcher function, for_each_type,
struct A {
...
-2
votes
1
answer
83
views
C++ Error: invalid conversion from 'long long int (*)()' to 'long long int (*)(int)'
I am trying to wrap a call of WinAPI function in the function that will automatically check for an error:
template<typename T = void,typename function, typename ...Args>
T winapi_wrapper(...
2
votes
0
answers
99
views
Addition operator for templatized aggregates
I made a class Point which can be 1D or 2D.
First, here is the code (it is compilable in C++17 on any online compiler):
#include <iostream>
#include <type_traits>
enum class Axis : ...
2
votes
3
answers
200
views
C++ Metaprogramning: accessing compile-time static class member from a CRTP c
How do I access a static constexpr inline variable from a CRTP-type class?
It beats me as to why T::X is not available in struct Templated?
template <typename T> struct Templated {
static ...
0
votes
1
answer
67
views
C++ TypeList with template types has index and type operations that don't work together
For a project I'm working on I have been trying to implement a typelist with template class.s as the possible types. The relevant parts of my implementation are below. It works well generally but it ...
2
votes
1
answer
69
views
How to convert a tuple of types to a tuple of class template instantiations, using the tuple types as template arguments
Consider the following types.
template<int data_>
struct CompileTimeData{ static constexpr int data = data_; };
template<int data>
struct S{};
In C++17, how does one convert a tuple of ...
0
votes
2
answers
200
views
When is virtual dispatch faster than function templates in C++ runtime?
Most knows that template meta programming is in general faster than virtual dispatch in C++ due to types of templates were decided in compile time while virtual functions required runtime lookup on ...
0
votes
0
answers
82
views
Calculate structure/class size at compile time based on list of members
This task arised while implementing a system for counting memory used by objects.
I have calcClassSize function that accepts an object of type S and a parameter pack of all S fields. The parameter ...
2
votes
2
answers
278
views
Get a compile-time index to a std::variant when visiting it? [duplicate]
I have a std::variant of possibly repeated types, and I would like to increment the active index of the variant.
Here is an example demonstrating what I would like to do:
template <typename... Ts&...
3
votes
1
answer
123
views
How to separate type deduction from function parameter declaration?
I have measured a significant speedup in my application if my function foo accepts primitive types by value instead of by universal (forwarding) reference. The speedup is lost however, if non-...
2
votes
1
answer
118
views
How to split variadic template arguments without additional copies?
Consider the following C++23 code:
#include <tuple>
#include <cstdio>
#include <iostream>
struct W1
{
int n;
bool b;
W1(int x, bool f) : n(x), b(f) {}
};
struct W2
{
...
0
votes
1
answer
81
views
Is there a C++20 concept that will be called when a function is used from a certain library?
I want all calls to functions from a certain library to be wrapped with a call to my function.
For example, whenever a function from the library <glad/glad.h> is called, my function log() is ...
4
votes
2
answers
208
views
With C++ meta programming, how to figure out whether you are in a class method or static/global function?
Is there a way to figure out whether the current scope is a global function/static class-method or a class-method?
I thought of the existence of this, but I'm unable to find a technique which doesn't ...
3
votes
2
answers
98
views
Design considerations for type traits for a set of related types
The C++ standard library provides std::char_traits<T> to unify the handling of character data without regard to the specific character type used.
It also provides <type_traits> for more ...
1
vote
0
answers
47
views
How to avoid code duplication when testing many types against many concepts? [duplicate]
I want to check if a (long) list of types (in this example just char and int) fulfil a (long) list of C++20 concepts (in this example just is_foo and is_bar). I currently approach this is as follows:
#...
4
votes
4
answers
260
views
std::integer_sequence in a lambda before C++20
I have started using this type of construction, which depends on C++20's explicit template parameters for lambdas:
template<typename... Ts>
struct Foo
{
std::tuple<Ts...> bars;
auto ...
1
vote
1
answer
90
views
Cannot expand a tuple pair in lockstep for a function call in index_sequence context
The question is simple (but not so the solution I guess...).
I want to apply a function to index 0 of n tuples with args, later to index 1
of tuple the same tuples with args... and so on.
Given this:
...
14
votes
1
answer
817
views
Interpretation of two complex requires clauses
This post talks about a method that counts the number of members in class:
struct UniversalType { template<typename T> operator T(); };
template<typename T, typename... A0>
consteval ...
1
vote
2
answers
167
views
How to interleave C++ template parameter packs
I have this c++ template function
template <int N, int B, auto... Args>
void somefunc() {
// do work...
}
, which I can call like this:
somefunc<1, true, 2, false>();
I want to ...
2
votes
1
answer
87
views
Try to replace old boost with new one in old code and get error, "type": is not a member of boost::mpl::eval_if
I tried to compile old code (~15 years old - luabind) that uses very old boost version (sth like 1.30) with new version of boost 1.86.
I replaced some apply_if usages with eval_if like it is suggested ...
0
votes
3
answers
199
views
Type trait that sums values of several integral constant types
From several integral constants, it is possible to define a trait Sum that returns the sum of all constants. For instance, we can define the following:
#include <type_traits>
template<...
1
vote
3
answers
98
views
Pass combination of brace-enclosed initializer lists and vectors to a variadic template
I want to write a table class which takes as argument multiple columns to create the table. This is my attempt to do so.
template <typename T = double>
class table
{
private:
using type = ...
1
vote
1
answer
70
views
Constexpr-check using SFINAE for std::variant
Is it possible to define a function which provides inspection of its templated argument with a specialization for std::variant, without actually including <variant>? Of course for the ...
2
votes
1
answer
76
views
Selection between classes at runtime for instantiation
I want to construct an object of a type which is selected at runtime based on an enum class value. The classes A and B have multiple constructors with various numbers of arguments which may be used at ...
0
votes
1
answer
74
views
How to use enable_if to do template specialisation with indefinite args
What I want to achieve
The motivation is I want to have template with form
template<typename T, typename ...Args>
where T is some class type. And I want a specialised version of this template ...