stdtuple

Can I get the return type of multiple chained functions calls?

霸气de小男生 提交于 2019-12-11 07:54:43
问题 I would like to store functions in an ordered collection and then apply all of them to a certain collection, which would result in obtaining heavily modified values, stored in another collection. My initial attempt consisted of creating an std::tuple of said functions and trying to get the result type ( std::invoke_result ) of applying all of them to a certain type: int main() { auto multiply = [](const auto arg){ return arg * arg; }; auto change = [](const auto arg){ return std::vector{arg};

Variadic template parameter pack to accept only unsigned ints or size_t as its type

本小妞迷上赌 提交于 2019-12-11 07:31:17
问题 I'm trying to use a set of template classes with a variadic parameter. I have several options ahead of me that I could choose from. Before any of my templates are declared or defined I currently have these prototypes: I'm familiar with templates but I haven't had much experience with variadic types when working with templates so the syntax does get a little confusing to me at times. Being that they are all empty shells they do currently compile. template<typename ClassType, typename... Args>

nested std::forward_as_tuple and segmentation fault

浪子不回头ぞ 提交于 2019-12-11 00:29:35
问题 My actual problem is a lot more complicated and it seems extremely difficult to give a short concrete example here to reproduce it. So I am posting here a different small example that may be relevant, and its discussion may help in the actual problem as well: // A: works fine (prints '2') cout << std::get <0>(std::get <1>( std::forward_as_tuple(3, std::forward_as_tuple(2, 0))) ) << endl; // B: fine in Clang, segmentation fault in GCC with -Os auto x = std::forward_as_tuple(3, std::forward_as

Generically call member function on each element of a tuple

时光总嘲笑我的痴心妄想 提交于 2019-12-06 08:20:37
问题 Step one: expand a tuple and pass elements to a function: I have a function which takes N parameters void func(int, double, char); and a tuple with the matching types std::tuple<int, double, char> tuple; As per this stackoverflow question, I am able to expand the tuple and call the function. Step two: expand a tuple and pass result of calling a member function on each of the elements to a function: Taking it a step further, my tuple contains multiple instances of a class template: template

Applying func to elements in std::tuple in the natural (not reverse) order

隐身守侯 提交于 2019-12-05 23:27:03
问题 I need to call a - template or overloaded - function for each element in an arbitrary tuple. To be precise, I need to call this function on the elements as they are specified in the tuple. For example. I have a tuple std::tuple<int, float> t{1, 2.0f}; and a functional class Lambda{ public: template<class T> void operator()(T arg){ std::cout << arg << "; "; } }; I need some struct/function Apply , which, if called like Apply<Lambda, int, float>()(Lambda(), t) would yield: 1; 2.0f; and NOT 2.0f

What is the reason for `std::make_tuple`?

烂漫一生 提交于 2019-12-05 12:04:29
问题 I mean why does std::make_tuple exist? I know that there are situations where the function reduces the amount of characters you have to type because you can avoid template parameters. But is it the only reason? What makes std::tuple special that the function exists while other class templates haven't such function? Is it only because you may use std::tuple more often in such situations? Here are two examples where std::make_tuple reduces the amount of characters: // Avoiding template

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

孤人 提交于 2019-12-05 04:08:04
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<int>>; using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<

Why can't std::tuple be element-wise constructed with a std::tuple of compatible types?

本秂侑毒 提交于 2019-12-05 02:20:14
I can't initialize std::tuple elements element-wise from a std::tuple of compatible types. Why doesn't it work as with boost::tuple ? #include <tuple> #include <boost/tuple/tuple.hpp> template <typename T> struct Foo { // error: cannot convert 'std::tuple<int>' to 'int' in initialization template <typename U> Foo(U &&u) : val(std::forward<U>(u)) {} T val; }; int main() { boost::tuple<Foo<int>>{boost::tuple<int>{}}; // ok auto a = boost::tuple<int>{}; boost::tuple<Foo<int>>{a}; // ok std::tuple<Foo<int>>{std::tuple<int>{}}; // fails with rvalue auto b = std::tuple<int>{}; std::tuple<Foo<int>>{b

Enable std::get support on class

寵の児 提交于 2019-12-04 18:19:06
问题 What are the templates that I have to specialize to support std::get? struct MyClass { int a; }; template <const size_t I> struct MyContainer { MyClass array[I]; }; What do I have to specialize to be able to do: MyContainer<16> mc; std::get<0>(mc); 回答1: std::get is not a customization point for the standard library; the three function template overloads (for pair , tuple and array ) do not explicitly allow for user-defined overloads, so 17.6.4.2.1p1 applies and adding a declaration of your

Requirements for std::ignore

北城余情 提交于 2019-12-04 17:56:36
问题 C++11 introduces an object called std::ignore : const /* unspecified */ ignore; For brevity, let typedef decltype(std::ignore) T; From what I can tell, the only requirement for T is that it is CopyAssignable , due to the specification of std::tie [C++11, 20.4.2.4:7]. In g++-4.8, I find that T is additionally DefaultConstructible (e.g., T x; compiles). Is this implementation-defined behavior? (If there are other requirements on T that I have missed, please elaborate.) 回答1: The standard has no