Questions tagged [variadic-templates]
Variadic templates are templates that take a variable number of parameters.
3,940
questions
0
votes
1
answer
61
views
Overload variadic function for a single specific type using C++17
I want to overload (or specialize) a variadic function, such that the overload (or specialization) takes only arguments of a specific type. In C++20 I would write:
#include <type_traits>
#...
0
votes
2
answers
108
views
Cast a function arguments to a given tuple element types
Is there a simple way to implement a function like this
template <typename Tuple, typename... Args>
constexpr auto make_similar_tuple(Args&&... args);
that casts its arguments to a ...
2
votes
1
answer
70
views
Can I use `enable_if` on a non-type template parameter pack inside the class definition?
I would like to template a class on a possibly fixed number of bools, count them, and use that number as the template parameter to a base class. Something like this
template <int Dimensionality> ...
3
votes
1
answer
94
views
c++20 partial class specialization with concepts and variadic template args
Looking to understand why the code below results in:
class template partial specialization is not more specialized than the primary template
template <typename T, std::integral... Us>
struct ...
2
votes
2
answers
76
views
How to loop through a list of variadic arguments [duplicate]
When I tried to iterate over variadic arguments via the following function
I don't understand '{(Print(Param),0)... } 'where 0 is used
#include<iostream>
using namespace std;
template<...
1
vote
1
answer
52
views
How to verify that two nested variants share the same type
The code uses nested std::variants to model messages:
class A{};
class B{};
class C{};
class D{};
using CMD1 = std::variant<A, B>;
using CMD2 = std::variant<C, D>;
using CMD = std::variant&...
0
votes
2
answers
88
views
CTAD with variable number of template arguments
For unit testing, I wrote a class ScopeSet which changes the values of variables in the current scope, and at the end of the scope the original values are set back.
Usage example:
int i = 1;
double d =...
-1
votes
0
answers
36
views
template parameter "Param" cannot be used because it follows a template parameter pack and cannot be deduced from the function parameters of "func"
I'm trying to learn about variadic template functions and I've encountered a problem while making the function work. basically when I try compiling the code, MSVC gives the error :
error C3547: ...
1
vote
1
answer
67
views
Parameter pack which only contains types convertible to T
Consider the following simple vector class:
#include <array>
#include <concepts>
template<typename T, std::size_t D>
class vector
{
public:
vector() = default;
vector(...
0
votes
0
answers
23
views
Variadic Template Argument Function with Concepts Failed to be Invoked
I needed a function that can accept variable no. of arguments. Each one, a pair of the same type. I used this to define the concept.
With the following definition of variadic template argument ...
1
vote
2
answers
70
views
Is there a way to store a variadic template of a specific type C++
Lets say I have the following scenario where I am printing templates of type std::size_t.
template<std::size_t st>
void print_sizes(){
std::cout << st << std::endl;
}
template&...
-1
votes
1
answer
47
views
Variadic Template deduction failed
I am trying to make a pipeline of operations type-safe. The code below is greatly simplified, but I believe it has the essential parts.
I have tried most combinations and orders of the variadic packs,...
1
vote
1
answer
69
views
Parameter pack cannot be expanded
template<integral auto n1, decltype(n1) n2, class callable_t, class... args_t> requires (n1 <= n2)
constexpr void const_for(callable_t&& callable, args_t&&... args)
{
...
0
votes
1
answer
46
views
C++ variadic template function ERROR C2672 'no matching overloaded function found'
I am currently trying to familiarize myself with variadic templates in c++, but I've kinda run into a wall of some sort, where every recursive implementation gives me error C2672. Even examples I've ...
1
vote
1
answer
35
views
C++ non-type variadic template pack not working with macro function
I have a small example below where the first one works just fine, but then the second one enacts an error where I get the error no member named 'handle_func' in 'test_nontype_variadic'
Working example:...
0
votes
1
answer
49
views
C++ variadic template 'contains no parameter packs' when constexpr if should avoid?
I am using some c++ 20 features such as the fold operator to gain the size of a variadic template at compile time. I am then using this size in a constexpr if to avoid any usage of the variadic ...
0
votes
0
answers
52
views
Is there a way to pass the first N elements from a variadic template argument to another function c++?
Currently, I would use the following code below that mainly uses tuples to pass the first N variadic template arguments to a function. I was wondering if there would be a more memory-efficient or just ...
2
votes
1
answer
85
views
How can I get the first N elements of a tuple c++?
lets say I had a function like the following, how could I get the first n elements of a tuple?
template<std::size_t N, class... Ts>
void foo(Ts... ts){
std::tuple<Ts...> all_elements(ts....
1
vote
1
answer
66
views
Can you have a default argument with a class function with variadic templates?
The following is entirely valid if I wanted to have a default argument using variadic templates. I was wondering if something similar could be replicated within a class:
inline static constexpr auto ...
1
vote
3
answers
62
views
Specialize variadic template to skip non-reference parameters
I am writing a wrapper for some client-server function calls, and I need a way to set some output parameter before or after the actual call. Sample code follows:
#include <iostream>
#include <...
3
votes
3
answers
69
views
Variadic Templates with Concepts
I've tested variadic templates inside a function, which I developed step by step.
template <typename... Ts>
void foo (Ts...) noexcept {}
// Test code
int i {123};
int j {456};
foo(1,2,3,i,j);
...
0
votes
2
answers
68
views
Double fold expression to replace branching statement
I am trying to figure out how to use fold expressions ( or other metaprogramming techniques) to get rid off "if else" branches in the code snippet below.
template<typename ... Ts>
...
1
vote
1
answer
83
views
C++ data array initialization
I need to initialize an uint8_t array,
most of it is constant (fixed identifiers), some elements encode length of next section, and some elements are variable.
uint8_t data[] = {
0x01, 0x05, 0x06, ...
1
vote
1
answer
69
views
Use parameter pack in template parameters list
I want to write an abstract Pipeline class that gets some functions with a specific signature as template parameters and runs them depending on the parse stage for input data. this is the code that I ...
0
votes
1
answer
138
views
How to implement a readable template for-loop syntax in cpp?
I want to code some header file whose inclusion allows me to then write template for-loops in an accessible syntax. How do I implement this? (cf. at the bottom for my best attempt).
There follows an ...
0
votes
0
answers
74
views
Problem with using non type parameter class template
I am trying to create some type packing, value packing library for template meta programming usage. I have trouble with some template template non type parameter:
#include <type_traits>
...
4
votes
3
answers
140
views
With C++ CRTP, how do I infer the parameters of a function in the derived class?
I'm trying to infer the return type and args of a function from within the CRTP base class in order to "wrap" functors.
Starting with the basic CRTP pattern:
template<typename Deriv>
...
1
vote
0
answers
40
views
C++11 compile time calculation of constant array
I have class with variadic template parameters like this:
template <uint16_t N1, uint16_t N2, uint16_t... Ns>
class TClass
{
public:
static constexpr std::size_t S = sizeof...(Ns) + 2;
...
0
votes
0
answers
13
views
From template<typename... Types> void func(Types...args) { ... } the value of sizeof...(Types) and sizeof(args) can be different?
template <typename... Types>
void func(Types... args) { }
From above code, the value of sizeof...(Types) and sizeof...(args) can be different?
I can't find any case that two values are ...
0
votes
1
answer
40
views
Function parameter pack specialization
template <typename T, typename ... Ts>
void foo(T , Ts ... )
{
}
template <typename ... Ts, typename T>
void bar(T , Ts ... )
{
}
int main()
{
foo<int, char>(1, 'c'); // well ...
0
votes
1
answer
76
views
Generate a std::tuple of std::function from variadic templates
Suppose a function like
template <typename ...ts>
void f(std::tuple<ts...> & data);
I would like to generate a std::tuple of std::function, each with this signature: std::function<...
-2
votes
2
answers
83
views
Is there a way to retrieve the inner types of a type using variadic templates in C++?
Assume I have a type using variadic templates:
template <typename... Args>
struct Outer
{
// using Inner = something that captures ...Args ???;
}
How could I define Inner such that I could ...
1
vote
1
answer
76
views
template function override
I am trying to create a template speacialization for a function which is based on a type
So for eg for the type number I have
template <class ElementType, typename = typename TEnableIf<TNot<...
3
votes
2
answers
114
views
Variadic templates and constraints
I have a variadic template function that takes as parameters file name, delimiter, and non-specified number of containers as columns. This function then parses to file all values in containers that ...
1
vote
1
answer
23
views
Typescript - How to infer correct subclass-type from dynamically created object
This is a follow up of this question.
What I am trying to achive is:
passing multiple sub-classes to a function is there a way to return an object that as a "schema" with the types of sub-...
1
vote
2
answers
74
views
Variadic Templated class
Is there a way in C++ to create a templated class that takes any number of arguments in the constructor and can get those when needed?
Example:
#include <string>
template<size_t Size, ...
2
votes
0
answers
41
views
template template variadic pack of concepts [duplicate]
I have a collection of concepts:
template <typename type_t>
concept boolean_c = std::is_same_v<std::decay_t<type_t>, bool>;
template <typename type_t>
concept integer_c = !...
0
votes
3
answers
94
views
Why I need default sum function, for a variadic template sum?
I want to calculate the sum of any number of arguments given to function sum. Assuming that integers given to the function will satisfy operator+.
If I comment out the function sum() (the one having ...
1
vote
1
answer
121
views
C++20: Encode and decode parameter pack as a variable to be used by varargs
Not sure if the following is possible in C++20 after a long and exhaustive search but asking anyway:
Using C++20 instead of C's va_list, can I:
(1) Encode arbitrary argument lists passed to a variadic ...
0
votes
1
answer
86
views
How could extract parameter pack from variadic template type (e.g. std::tuple) in C++ 11
With C++ 11 How can I extract a variadic argument of a template type as a parameter pack?
Say we have template specialization type Argument_t
...
template<typename F>
using Argument_t = ...
1
vote
2
answers
105
views
Concept that ONLY allows non-type template parameters that are members of certain namespace
I have a namespace Bar and a namespace Bar::Inner (declared within Bar).
// Bar
namespace Bar {
size_t num = 32;
// Bar::Inner
namespace Inner {
size_t num = 4;
};
};
...
6
votes
1
answer
97
views
Return the addition of variadic parameter pack
Let's say I have a function sum that takes a variadic parameter pack.
This function needs to ADD UP all of the parameters of the parameter pack using the operator +.
NOTE: It CANNOT use the operator +...
2
votes
0
answers
63
views
C++: How to generate all possible template instantiations for a fixed set of template types?
I need to do as follows:
I have a function of the form
template<typename T1, typename T2, typename T3>
void func(string a, T1 arg1, T2 arg2, T3 arg3, bool b);
But the template types would only ...
9
votes
2
answers
254
views
Implementing variadic Max function in C++20
Despite, the fact, we have std::max, I wanted to try if it is possible to make a Max version that takes variadic arguments and calls the Max recursively for finding the max element.
I saw similar ...
5
votes
3
answers
86
views
Unpack variadic template to initializer_list and call two functions at once
I have two vectors:
std::vector<int> v1{ 1, 2, 3 };
std::vector<int> v2{ 4, 5, 6 };
I want to create an object of std::initializer_list which holds iterators to the first and last ...
15
votes
2
answers
522
views
How to perfect forward variadic template args with default argument std::source_location?
I want to track where the creation of an object occurs using std::source_location. The following is a simplified example without the extra code to account for copy and movement of a Tracked object.
#...
4
votes
2
answers
111
views
C++ std::tuple<Ts...> to std::tuple<std::vector<T>...>>
Given a variadic template, I would like to define an object that would be a tuple of vector of each type in the variadic template.
Something like:
template <typename... Ts>
class C {
std::...
1
vote
1
answer
102
views
Why does vector take an initializer_list in its constructor instead of using a variadic template?
In this code
vector<std::string> vec = { "long string", "exterminate" };
an initializer_list of std::string is created and each element in copied into vec. This is ...
2
votes
1
answer
131
views
Sorting Variadic Template Arguments Using Quick Sort
Introduction
What I want to do is: Regardless of the order of template arguments, I want the objects which contain the same templates, to also have the same return value of Family::identifier(). What ...
0
votes
1
answer
92
views
C++ fold expressions, how to apply it with a function instead of an operator?
I have a set of messages, some of which have variable sizes but still with fixed maximum sizes. I want to get the maximum size amongst that set of messages.
template< typename... Messages >
...