stdtuple

How to create a tuple of vectors of type deduced from a variadic template in C++17?

▼魔方 西西 提交于 2021-02-19 05:04:11
问题 I have implemented a collection class that converts a vector of tuples to a tuple of vectors (it is essentially an AOS to SOA conversion). This code works for this example of two template classes. I was trying to make it more generic by using variadic templates. In order to do that I need to create the type for the member variable m_col . In C++17, is it possible to convert a tuple to a tuple of vectors? So the type of the member variance m_col in this example will be generated automatically

access tuple elements by index c++11

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-08 05:08:05
问题 It's not secret, std::get<i>(tuple) annoys many programmers. Instead of it, I want use something like tuple[i] . So I tried to simulate it. #include <iostream> #include <type_traits> #include <tuple> template < int > struct index{}; template< char ... > struct combine; template<> struct combine<> : std::integral_constant< int , 0>{}; constexpr int ten(size_t p)noexcept { return p == 0 ? 1 : 10 * ten(p-1); } template< char c, char ... t> struct combine<c, t...> : std::integral_constant< int,

Produce std::tuple of same type in compile time given its length by a template argument

扶醉桌前 提交于 2020-05-25 06:19:46
问题 In c++, how can I implement a function with an int template argument indicating the tuple length and produce a std::tuple with that length? E.g. func<2>() returns std::tuple<int, int>(); func<5>() returns std::tuple<int, int, int, int, int>(). 回答1: Here is a recursive solution with alias template and it's implementable in C++11: template <size_t I,typename T> struct tuple_n{ template< typename...Args> using type = typename tuple_n<I-1, T>::template type<T, Args...>; }; template <typename T>

Why can't I use a std::tuple in a constexpr lambda function

懵懂的女人 提交于 2020-04-17 03:12:32
问题 I have the following code: #include <string_view> #include <array> #include <tuple> struct Variable { size_t index; std::string_view name; std::tuple<float, float> bounds; }; constexpr std::array<Variable, 3> myarray = [](){ std::array<Variable, 3> res{}; std::array<std::string_view, 3> strings = {"myvar1", "myvar2", "myvar3"}; std::array<std::tuple<float, float>, 3> bounds = {{{0,1}, {1,2}, {2,3}}}; for (std::size_t i = 0; i != res.size(); ++i) { res[i] = {i, strings[i], bounds[i]}; } return

Confusion while deriving from std::tuple, can not handle std::get

蓝咒 提交于 2020-01-13 08:42:26
问题 My basic idea was to derive my own class from std::tuple to get some helper types inside like this: template <typename ... T> class TypeContainer: public std::tuple<T...> { public: using BaseType = std::tuple<T...>; static const size_t Size = sizeof...(T); TypeContainer(T... args):std::tuple<T...>(args...){}; using index_sequence = std::index_sequence_for<T...>; }; Now I try to use the code as follows: using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple

Sum the components of a tuple up by using std::get, std::tuple_size, std::tuple_element

╄→гoц情女王★ 提交于 2020-01-11 03:25:08
问题 I've got a custom class that has a tuple-like interface. Because I want my code to be as generic as possible, I thought that it would be a good idea to base my algorithms on the functions std::get , std::tuple_size , std::tuple_element so you just have to specialize these functions to use my algorithms. Let's call the concept that requires these function specializations Tuple . Now I am trying to sum up the components of a Tuple . The function declaration should be something like this:

Why is constexpr with std::forward_as_tuple not working? [duplicate]

允我心安 提交于 2020-01-05 07:11:27
问题 This question already has answers here : how to initialize a constexpr reference (3 answers) Constexpr Class taking const references not compiling (1 answer) constexpr begin of a std::array (1 answer) Closed 2 days ago . Why is the following not compiling? This is somehow counter-intuitive (not to say constexpr concepts are confusing): #include <tuple> int main() { constexpr const int a = 0; static_assert(a == 0, "Wups"); constexpr auto t2 = std::forward_as_tuple(a, a); } LIVE I assumed that

c++11 how to implement `std::string ToString(std::tuple<Args…> &t)`?

自作多情 提交于 2020-01-01 19:13:07
问题 I want a very friendly ToString function for many types, include the std::tuple . The function is like this: template <typename T> inline std::string ToString(const T &t) { std::stringstream ss; ss << t; return ss.str(); } template <typename... Args> inline std::string ToString(const std::tuple<Args...> &t) { std::stringstream ss; for (int i = 0; i < t.size(); i++) { ss << ToString(std::get<i>(t)) << " "; } return ss.str(); } The second part is wrong on grammar, how to implement it with c++11

std::tuple member by member comparison fails

假装没事ソ 提交于 2019-12-24 19:02:27
问题 I wanted to test this very interesting answer and came out with this minimal implementation: class A { enum M { a }; std::tuple<int> members; public: A() { std::get<M::a>(members) = 0; } A(int value) { std::get<M::a>(members) = value; } A(const A & other) { members = other.members; } int get() const { return std::get<M::a>(members); } bool operator==(A & other) { return members == other.members; } }; and a simple test: int main() { A x(42); A y(x); std::cout << (x==y) << std::endl; return 0;

Common base class breaks empty base class optimization for tuples

痴心易碎 提交于 2019-12-23 18:12:35
问题 gcc 4.7.1 does empty base class optimization for tuples, which I consider a really useful feature. However, there appears to be an unexpected limit to this: #include <tuple> #include <cstdint> #include <type_traits> class A { }; class B : public A { std::uint32_t v_; }; class C : public A { }; static_assert(sizeof(B) == 4, "A has 32 bits."); static_assert(std::is_empty<C>::value, "B is empty."); static_assert(sizeof(std::tuple<B, C>) == 4, "C should be 32 bits."); In this case, the last