stdtuple

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

假装没事ソ 提交于 2019-12-04 15:12:43
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 template ? And, how to implement the FromString like this: template <typename T> inline T FromString

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

陌路散爱 提交于 2019-12-04 05:27:29
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; 1; . Note that I know the solution, if a "raw" parameter pack is passed in to the function and I know

Enable std::get support on class

允我心安 提交于 2019-12-03 11:55:44
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); 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 own function template overload is undefined behaviour. Note that get as an unqualified name is a customization

Requirements for std::ignore

不羁岁月 提交于 2019-12-03 10:42:48
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.) The standard has no requirements on the type of ignore , besides the fact that it is a type that is distinct from all other

Making `std::get` play nice with SFINAE

删除回忆录丶 提交于 2019-12-03 05:12:48
std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/tuple:1290:14: fatal error: no matching function for call to '

Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?

走远了吗. 提交于 2019-12-01 16:21:19
I'm not clear why it is legal to assign tuple<X,Y>=pair<X,Y> But it is illegal to assign pair<X,Y>=tuple<X,Y> std::pair<int, double> x { 1 , 5.5}; std::tuple<int, double> y { 1 , 5.5}; int a; double b; std::tie(a,b) = x; std::tie(a,b) = y; x = y; // THIS LINE (line 12) y = x; // but this is fine ??? Shouldn't this be symmetrical? Using g++ 4.8.1 gives the following errors: tp.cpp:12:4: error: no match for operator= (operand types are std::pair<int, double> and std::tuple<int, double>) x = y; ^ tp.cpp:12:4: note: candidates are: In file included from /opt/gcc-4.8.1/include/c++/4.8.1/utility:70

Confused by default constructor description of std::tuple in the ISO C++ Standard

痞子三分冷 提交于 2019-12-01 15:44:36
The Standard says that std::tuple has the following member functions constexpr tuple(); explicit tuple(const Types&...); Can someone please explain what is supposed to happen for std::tuple<> ? I believe this is a minor error in the standard. Clearly, when the Types parameter pack is empty, the two constructor calls are equivalent and cannot be overloaded (see C++11 section 13). (Further note that the constructor using Types is not a member template either --if it was, then it would be a legal overload.). In other words, this code will not compile: template <typename... Types> struct Test {

Confused by default constructor description of std::tuple in the ISO C++ Standard

瘦欲@ 提交于 2019-12-01 13:51:31
问题 The Standard says that std::tuple has the following member functions constexpr tuple(); explicit tuple(const Types&...); Can someone please explain what is supposed to happen for std::tuple<> ? 回答1: I believe this is a minor error in the standard. Clearly, when the Types parameter pack is empty, the two constructor calls are equivalent and cannot be overloaded (see C++11 section 13). (Further note that the constructor using Types is not a member template either --if it was, then it would be a

How to get reference to an element of a std::tuple?

限于喜欢 提交于 2019-12-01 04:21:58
You can get the value of the n th element of an std::tuple using std::get<n>(tuple) . But I need to pass one element of that tuple as reference to a function. How do I get the reference to an element of a std::tuple ? std::get returns a reference(either const or non-const), so this works: void fun(int &a) { a = 15; } void test() { std::tuple<int, char> foo{ 12, 'a' }; fun(std::get<0>(foo)); } Demo here . get returns a reference, rvalue reference or const reference depending on the type of its argument. std::get returns a reference to the element at the specified position in the tuple. http:/

C++ std::get<variable> fails

北城以北 提交于 2019-12-01 03:13:47
问题 How do I use a variable to index into a tuple using std::get<> ? I have the following code: #include <iostream> #include <tuple> using namespace std; int main() { tuple<int, int> data(5, 10); for (int i=0; i<2; i++) { cout << "#" << i+1 << ":" << get<i>(data) << endl; } return 0; } and it fails with the following compiler error: prog.cpp: In function 'int main()': prog.cpp:10:39: error: the value of 'i' is not usable in a constant expression cout << "#" << i+1 << ":" << get<i>(data) << endl;