Questions tagged [stdtuple]
Use this tag for questions about the C++ standard library template std::tuple. It represents an ordered, heterogeneous sequence of objects. Also add the tag [c++] when using this tag.
419
questions
1
vote
2
answers
85
views
C++ synthesize linked list in constexpr
I have the following problem, consider a linked list like this:
struct Element {
struct Element* const next; // the const is required here
constexpr Element(struct Element* _nxt)
: next(_nxt)
{ }
...
0
votes
1
answer
53
views
How to inject std::basic_istream through constructor without explicitly specifying <CharT,Traits>
I'm writing templated CSVParser class, that reads data from any basic_istream object and converts it to std::tuple of given types:
template<class Ch, class Tr, class... Types>
class CSVParser {
...
0
votes
1
answer
79
views
C++17 recursive flattening of nested std::tuple
I have a function unpack, which should construct an std::tuple from a variadic template expression. The function looks like this (including out-commented failed attemps):
template<typename T, ...
0
votes
1
answer
77
views
How to initialize a const primitive from a function returning a tuple with explicit type [duplicate]
I have a function that returns const values not by reference as in this question. I would like to call the function and initialize the const variables when I call the function but it isn't possible.
I ...
0
votes
1
answer
54
views
Casting std::any to std::tuple
I'm having this piece of code...
class TableModel {
public:
TableModel(std::vector<std::string> header = {},
std::string tableName = "")
: header_(header....
0
votes
1
answer
97
views
Copying elements of multiple source tuples to the destination tuple
I'm trying to assign values of all elements from multiple source tuples to elements of one destination tuple as follows:
#include <tuple>
template<std::size_t from_element, typename dstT, ...
2
votes
1
answer
74
views
Improve this std::index_sequence idiom code
I have programmed some serializing functions able to calculate the size, write into a memory buffer and read from a buffer of any struct/class that compliments with these features: containing a ...
0
votes
0
answers
67
views
Difference between instantiating a tuple with explicitly specified types using std::make_tuple and using the tuple constructor [duplicate]
Take for example the code snippet bellow that instantiates some tuple instances using the std::make_tuple function and the tuple constructor:
...
const auto integer = 0;
const auto text = std::string{...
1
vote
0
answers
39
views
libpqxx stream_to with std::vector
I use prepared statement to save 1 entity to database
_connection.prepare("save", "INSERT INTO public.table (col1, col2, col3) VALUES ($1, $2, $3)");
Then in my Save function I ...
0
votes
1
answer
126
views
How do I obtain the variadic arguments from an std::tuple to unpack for another std::tuple?
I am trying to convert a list of template arguments (enum class, not typenames) to corresponding typenames for the template arguments to an std::tuple. I'm guessing if I can get somehow name the ...
1
vote
0
answers
22
views
Glib Variant Tuple Deserialization
I am trying to de-serialize below Tuple, but getting issues while getting internal vector variant.
initial 2 values are retrieved but vector variant is giving me some trouble I am not able to ...
17
votes
1
answer
672
views
Why do I need to specify the type of a default constructed object in this situation?
I don't understand why in foobar below I need to specify std::vector<int>{} whereas in foobar2 I do not:
#include <iostream>
#include <memory>
#include <vector>
#include <...
2
votes
1
answer
75
views
Is Clang-Tidy correct regarding std::move when constructing std::tuple using `{}`?
I was implementing a function in C++, specifically a callable class object, designed to return a std::tuple containing a container of objects and a result. After some adjustments (see below) I have ...
4
votes
1
answer
117
views
Is it possible to swap the result of two std::tie calls
I have been trying for a while to find a standard compliant way to get something like this to compile:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
std::swap(std::tie(a, b), std::tie(c, d));
(The use ...
2
votes
2
answers
77
views
C++20 return a tuple in ternary statement [duplicate]
I have a small function that returns three values. I use a std::tuple to facilitate returning of multiple values.
What the function returns is determined by one variable, if the variable is not zero ...
2
votes
1
answer
130
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
votes
3
answers
79
views
Is it possible to retrieve an element from a tuple using a non-const variable and as_const()?
As title states, I'm wondering if it's possible to pass in a variable to std::get<>() of a tuple?
I have a header file, which contains a struct that holds numerous params (and functions) that ...
0
votes
1
answer
90
views
set a value in std::tuple using a function that takes an index to the std::get
If You want to read data from vector of std::tuple You can use :
#include <algorithm>
#include <iostream>
#include <sstream>
#include <tuple>
#include <variant>
#include &...
1
vote
0
answers
50
views
Traversing a std::tuple error when using 'auto' and 'std::make_tuple'
In Generate a std::tuple of std::function from variadic templates I learned an elegant solution for traversing a std::tuple, but I am facing an error I can not find out why. In the code below:
#...
0
votes
1
answer
78
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<...
4
votes
2
answers
113
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::...
2
votes
1
answer
45
views
Retrieving printf format parameter by type
The following lambda is supposed to return the string formatter for printf and alike at compiletime, however it doesn't seem to work as intended and I can't get behind it.
Demo
#include <array>
#...
-2
votes
2
answers
63
views
Select & build a compile-time tuple structure based on a runtime-provided mapping
I am working on a piece that requires some compile-time data structures to be created from a run-time provided mapping. If the runtime provided mapping matches the pre-defined compile-time pattern, ...
0
votes
1
answer
51
views
Why does optimizing with std::forward_as_tuple produce runtime errors?
Let's consider the following program:
#include <iostream>
#include <tuple>
using namespace std;
const int* f()
{
static int i = 5;
return &i;
}
int main()
{
auto [a] = ...
0
votes
3
answers
94
views
std::tuple data member in a concept
Continuing my studies in C++ concepts, I would like to define a std::tuple<t...> data member in a concept.
In the following code:
#include <concepts>
#include <iostream>
#include <...
3
votes
4
answers
256
views
Is there anyway to find std::tuple elements are all same type or not?
I would like to figure out true / false based on all of elements in std::tuple are same type or not.
for example
std::tuple<int, int, int> t = { 1, 1, 1};
auto A = all_same(t);
std::tuple<...
2
votes
1
answer
120
views
Use tuple in variadic template
I am designing a logger. I'll format for it. I made a design that I would predetermine the format string and then keep this data in a tuple and print it to the log.
The code below is working now, but ...
2
votes
0
answers
41
views
Can I deduce the type of function template arguments from aggegate-initialisation arguments without spelling out their type template?
I have a helper function f that receives a variadic number of tuple-like arguments, but I'd be okay restricting it to std::tuple. These are in practice always temporaries, so a typical call looks ...
1
vote
1
answer
108
views
How to create std::tie from std::vector?
Is there a way to create a std::tie from an std::vector's elements?
Or
In other words, dynamically create a std::tie at runtime.
2
votes
1
answer
111
views
Constrain to function objects with one reference parameter of any type [duplicate]
I'm probably scratching the possibilities of C++ a bit here. I have this tuple for each function that executes a function for every tuple element. Now I want to constrain the incoming function object ...
11
votes
2
answers
765
views
Initialize std::tuple with classes which have two or more arguments
#include <iostream>
class NoCopyMove {
public:
NoCopyMove(int a) : a_(a), b_(a) {}
NoCopyMove(int a, int b) : a_(a), b_(b) {}
NoCopyMove(const NoCopyMove&) = delete;
...
2
votes
1
answer
48
views
std::apply-ing sscanf into a tuple, tuple not fully updating
I have a structured string of data, and I want to parse it into a tuple. For each different kind of input string, the types and arrangement of the data can be different, so I want to use templates and ...
3
votes
3
answers
141
views
Remapping a tuple onto another tuple
I am attempting to convert between two types of std::tuples but I'm having trouble getting the implementation right. I want it to map if the type is the same, but it needs to allow duplicate types to ...
2
votes
0
answers
77
views
Is there a better way to get `std::tuple::operator==` to leverage `operator==` from another namespace?
I'm using a C struct (that I do not control) and wrote a freestanding operator== implementation for it in a namespace. I have another class that has that C struct as a member and that also has an ...
0
votes
0
answers
28
views
Types names as string to std::tuple instance [duplicate]
I have an **std::vector<std::string>** witch contains the names of types example **["int","float","string"]**, i want to create an std::tuple instance that reflect ...
0
votes
1
answer
277
views
entt basic_registry::get<>() explanation
Currently using g++11.3.0, C++20.
Could anyone explain how the basic_registry's get<>() template function is able to function such that when retrieving a single component through a call like ...
0
votes
1
answer
92
views
C++20 Returning Non-type Variadic Template Tuple of References
Currently using g++11.3.0, C++20.
This is a follow up from a previous query.
I have a function foo that takes an arbitrary number of template arguments and returns a tuple that contains a value for ...
1
vote
1
answer
134
views
C++20 Returning Non-type Variadic Template Tuple
Currently using g++11.3.0, C++20.
I'm trying to compile the code below, where the function foo takes an arbitrary number of template arguments and returns a tuple that contains a value for each ...
0
votes
1
answer
58
views
Is an array in a member initializer list for a variadic struct possible?
I created a variadic struct based on a std::tuple.
Then, I would like to use a C-style array in the member initializer list with automatic type deduction / class template argument deduction.
I created ...
-2
votes
1
answer
125
views
Is there a way to pass variables to std::make_tuple<double, char, std::string>(var1, var2)?
I want to pass variable names to std::make_tuple(), but it wouldn't let me. I'm using C++14, is there a way to achieve what I want?
std::tuple<int> get_student(int id)
{
int gpa = 3;
...
0
votes
1
answer
76
views
Are value categories preserved inside a tuple (perfect forwarding through tuples)?
I wrote the following snippet to test if I could perfectly forward values through a tuple and std::invoke. However the generated assembly looks kind of odd.
Demo
#include <concepts>
#include <...
0
votes
1
answer
89
views
Cannot construct tuple from atomic object in C++
I am new to atomic in C++ and trying to make a tuple from atomic objects. I am getting a compile time error and I do not understand why. How can I resolve the error?
Created this test program
int main(...
1
vote
1
answer
60
views
aggregate-initializable tuple like data structure
I want to store multiple non-movable types in a single variable.
At the very first, I have tried std::tuple at the very first, but it fails.
#include <tuple>
template<typename T>
struct ...
0
votes
1
answer
138
views
Incomplete type std::tuple_size<T> even though T is complete?
In the following code I try to get the tuple size of a custom type that derives from std::tuple. But the compiler complains that std::tuple_size is incomplete... which I can't really understand since ...
0
votes
0
answers
16
views
Copying from std::tuple to thrust::tuple
How can I copy the members of std::tuple to thrust::tuple where both tuples are variadic templated? It is to be noted that the tuples are templated by the classes without default constructors.
so ...
0
votes
2
answers
119
views
Unable to store an std::type_info object in an std::tuple
I'm trying to initialize a tuple (using std::make_tuple), and I want to store an std::type_info in it as well, but I don't know why for some reason this simple looking piece of code doesn't compile.
I ...
1
vote
1
answer
98
views
How to detect overload resolution failure?
Consider the following code snippet:
#include <tuple>
#include <type_traits>
#include <utility>
#include <cstddef>
template <typename ...>
struct always_false : std::...
0
votes
3
answers
276
views
how can I print both positive and negative indexes together with its corresponding element?
In the following code I want to print in the way mentioned in the question what am I getting is this:
https://prnt.sc/oEjjTyr_dtdu
Tried this hoping they would come together with their corresponding ...
2
votes
1
answer
120
views
error use of deleted function when trying to pass rvalue to a tuple
Original context:
I am trying to pass a tuple of (object, expected_value_of_some_property) to a test function
I created a simple class to reproduce the error I am facing:
template <typename T>
...
1
vote
1
answer
114
views
How to use std::get in a tuple wrapper class?
I have an object that must store a tuple for some reason, something similar to this:
template<typename... Types>
class MultiStorer {
public:
tuple<Types...> my_tuple;
MultiStorer(...