tuples

Transform variadic template parameters to another types

你说的曾经没有我的故事 提交于 2019-11-30 03:43:13
How to transform types from variadic template parameters to another type? For example: template <typename... T> struct single { std::tuple<T...> m_single; }; template <typename... T> struct sequences { single<T...> get(size_t pos) { // I don't know how to convert here return std::make_tuple(std::get<0>(m_sequences)[pos]... std::get<N>(m_sequences)[pos]); } template <size_t Idx> std::vector< typename std::tuple_element<Idx, std::tuple<T...>>::type > get_sequence() { return std::get<Idx>(m_sequences); } std::tuple<T...> m_sequences; // std::tuple<std::vector<T...>> I don't know how to conver

std::get using enum class as template argument

巧了我就是萌 提交于 2019-11-30 03:03:09
问题 I'm using a std::tuple and defined a class enum to somehow "naming" each of the tuple's fields, forgetting about their actual indexes. So instead of doing this: std::tuple<A,B> tup; /* ... */ std::get<0>(tup) = bleh; // was it 0, or 1? I did this: enum class Something { MY_INDEX_NAME = 0, OTHER_INDEX_NAME }; std::tuple<A,B> tup; /* ... */ std::get<Something::MY_INDEX_NAME> = 0; // I don't mind the actual index... The problem is that, as this compiled using gcc 4.5.2, I've now installed the 4

Using Tuples in Ruby?

我的梦境 提交于 2019-11-30 02:36:55
Does anyone use tuples in Ruby? If so, how may one implement a tuple? Ruby hashes are nice and work almost as well, but I'd really like to see something like the Tuple class in Python, where you can use . notation to find the value for which you are looking. I'm wanting this so that I can create an implementation of D , similar to Dee for Python. OpenStruct ? Brief example: require 'ostruct' person = OpenStruct.new person.name = "John Smith" person.age = 70 person.pension = 300 puts person.name # -> "John Smith" puts person.age # -> 70 puts person.address # -> nil Based on the fact that you

How to make a function that zips two tuples in C++11 (STL)?

好久不见. 提交于 2019-11-30 01:55:22
I recently ran across this puzzle, was finally able to struggle out a hacky answer (using index arrays), and wanted to share it (answer below). I am sure there are answers that use template recursion and answers that use boost ; if you're interested, please share other ways to do this. I think having these all in one place may benefit others and be useful for learning some of the cool C++11 template metaprogramming tricks. Problem: Given two tuples of equal length: auto tup1 = std::make_tuple(1, 'b', -10); auto tup2 = std::make_tuple(2.5, 2, std::string("even strings?!")); How do you create a

Is C# 4.0 Tuple covariant

倖福魔咒の 提交于 2019-11-30 01:54:57
问题 (I would check this out for myself, but I don't have VS2010 (yet)) Say I have 2 base interfaces: IBaseModelInterface IBaseViewInterface And 2 interfaces realizing those: ISubModelInterface : IBaseModelInterface ISubViewInterface : IBaseViewInterface If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface> . In C# 3 I can't do this even though the sub interfaces realize the

Does Haskell have variadic functions/tuples?

泄露秘密 提交于 2019-11-30 01:37:13
The uncurry function only works for functions taking two arguments: uncurry :: (a -> b -> c) -> (a, b) -> c If I want to uncurry functions with an arbitrary number of arguments, I could just write separate functions: uncurry2 f (a, b) = f a b uncurry3 f (a, b, c) = f a b c uncurry4 f (a, b, c, d) = f a b c d uncurry5 f (a, b, c, d, e) = f a b c d e But this gets tedious quickly. Is there any way to generalize this, so I only have to write one function? Try uncurryN from the tuple package. Like all forms of overloading, it's implemented using type classes. In this case by manually spelling out

What is the difference between assigning to std::tie and tuple of references?

爷,独闯天下 提交于 2019-11-30 01:31:38
问题 I am a bit puzzled by the following tuple business: int testint = 1; float testfloat = .1f; std::tie( testint, testfloat ) = std::make_tuple( testint, testfloat ); std::tuple<int&, float&> test = std::make_tuple( testint, testfloat ); With std::tie it works, but assigning directly to the tuple of references doesn't compile, giving "error: conversion from ‘std::tuple<int, float>’ to non-scalar type ‘std::tuple<int&, float&>’ requested" or "no suitable user-defined conversion from std::tuple

How to check if a tuple contains an element in Python?

喜夏-厌秋 提交于 2019-11-30 01:02:29
问题 I tried to find the available methods but couldn't find it. There is no contains . Should I use index ? I just want to know if the item exists, don't need the index of it. 回答1: You use in . if element in thetuple: #whatever you want to do. 回答2: if "word" in str(tuple): # You can convert the tuple to str too i has the same problem and only worked to me after the convert str() 回答3: Be careful with that: return Oops. use Set: d= {...} def simha(): d = ('this_is_valid') b = 'valid' if b in d:

Create a tuple from a string and a list of strings

夙愿已清 提交于 2019-11-30 00:47:23
问题 I need to combine a string along with a list of strings into a tuple so I can use it as a dictionary key. This is going to be in an inner loop so speed is important. The list will be small (usually 1, but occasionally 2 or 3 items). What is the fastest way to do this? Before: my_string == "foo" my_list == ["bar", "baz", "qux", "etc"] After: my_tuple == ("foo", "bar", "baz", "qux", "etc") (Note: my_list must not be altered itself). 回答1: I can't speak for performance, but this is definitely the

Why does Django use tuples for settings and not lists?

我怕爱的太早我们不能终老 提交于 2019-11-30 00:46:40
问题 Quoting this answer: Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order. This makes sense to me. But why does Django use tuples and not lists for settings? Example: INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django