### Constructing Boost.Tuple Objects Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Illustrates different methods for constructing `tuple` objects. Examples show initialization with zero, one, or all elements, demonstrating default initialization for unspecified elements and explicit value provision. ```cpp tuple() tuple(1) tuple(1, 3.14) ``` -------------------------------- ### Valid Boost.Tuple Type Instantiations Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Demonstrates various valid ways to instantiate the `tuple` template in C++. These examples showcase different combinations of fundamental types, pointers, references, and user-defined types as tuple elements. ```cpp tuple tuple tuple tuple > tuple, bool, void*> ``` -------------------------------- ### Boost Tuples Namespace Structure and Usage (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/design_decisions_rationale Demonstrates the namespace structure for Boost tuples, allowing common names like tuple, make_tuple, tie, and get to be accessible directly under the boost namespace for application programmers. It also highlights how internal library details remain in the boost::tuples subnamespace. This setup simplifies tuple creation and manipulation while managing potential name clashes. ```cpp namespace boost { namespace tuples { // All library code ... } using tuples::tuple; using tuples::make_tuple; using tuples::tie; using tuples::get; } ``` -------------------------------- ### Tuple Element Indexing Examples Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/design_decisions_rationale Demonstrates different syntaxes for accessing tuple elements. It highlights the preference for 0-based indexing in C++ and discusses the potential for 1-based indexing with custom constants. ```C++ a.get<0>() == a.get(_1st) == a[_1st] == a(_1st); ``` -------------------------------- ### Tuple Return vs. Non-Const Reference Parameters (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Illustrates the potential performance difference between returning a tuple and using non-const reference parameters for multiple return values. Function 'f1' uses reference parameters, while 'f2' returns a tuple. The example shows how 'tie' is used to unpack the tuple. ```C++ void f1(int&, double&); tuple f2(); int i; double d; ... f1(i,d); // #1 tie(i,d) = f2(); // #2 ``` -------------------------------- ### Access Tuple Elements with get Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Tuple elements can be accessed using the `get()` member function or the non-member `get(t)` function, where N is the element index. Compile-time checks prevent out-of-bounds access. Note potential MSVC++ specific requirements. ```C++ double d = 2.7; A a; tuple t(1, d, a); const tuple ct = t; int i = get<0>(t); i = t.get<0>(); // ok int j = get<0>(ct); // ok get<0>(t) = 5; // ok // get<0>(ct) = 5; // error, can't assign to const double e = get<1>(t); // ok get<1>(t) = 3.14; // ok // get<2>(t) = A(); // error, can't assign to const // A aa = get<3>(t); // error: index out of bounds ++get<0>(t); // ok ``` -------------------------------- ### Accessing Tuple Elements with get (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Tuple elements can be accessed using `t.get()` or `get(t)`, where `N` is the element index. The return type depends on whether the tuple is const. Index violations are detected at compile time. Note: MSVC++ may require explicit namespace qualification for `get`. ```cpp double d = 2.7; A a; tuple t(1, d, a); const tuple ct = t; int i = get<0>(t); i = t.get<0>(); // ok int j = get<0>(ct); // ok get<0>(t) = 5; // ok // get<0>(ct) = 5; // error, can't assign to const double e = get<1>(t); // ok get<1>(t) = 3.14; // ok // get<2>(t) = A(); // error, can't assign to const // A aa = get<3>(t); // error: index out of bounds ++get<0>(t); // ok, can be used as any variable // For MSVC++ 6.0: // tuples::get(a_tuple) ``` -------------------------------- ### Boost Tuple Construction and Access in C++ Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Demonstrates construction and element access using Boost tuple with type-based template syntax. Functionally equivalent to the hand-written tuple class above, but implemented through the library. A decent compiler produces identical machine code for both approaches. ```cpp tuple t(A(), B(), C()); t.get<0>(); t.get<1>(); t.get<2>(); ``` -------------------------------- ### Include Boost.Tuple Header Source: https://www.boost.org/doc/libs/latest/libs/tuple/index This code snippet shows the necessary header file to include for using the core Boost.Tuple functionality. It's the foundational include for all tuple operations. ```cpp #include "boost/tuple/tuple.hpp" ``` -------------------------------- ### Include Boost.Tuple Input/Output Operators Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Include this header to enable stream input and output operations for Boost.Tuple objects. This header implicitly includes `boost/tuple/tuple.hpp` and `boost/tuple/tuple_comparison.hpp`. ```c++ #include "boost/tuple/tuple_io.hpp" ``` -------------------------------- ### Constructing Tuples with Non-Copyable Elements Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Demonstrates tuple construction when elements are non-copyable or not default constructible. It shows that providing initial values is necessary for such types, while default construction might fail. ```cpp class Y { Y(const Y&); public: Y(); }; char a[10]; tuple(a, Y()); // error, neither arrays nor Y can be copied tuple(); // ok Y y; tuple(a, y); // ok ``` -------------------------------- ### Constructing Boost.Tuple Objects Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Illustrates various ways to construct `boost::tuple` objects. Constructors can be invoked with fewer arguments than the total number of elements, in which case the remaining elements are default-initialized. Explicit initialization is required for reference types. ```c++ tuple() ``` ```c++ tuple(1) ``` ```c++ tuple(1, 3.14) ``` ```c++ double d = 5; tuple(d) // ok ``` ```c++ double d = 5; tuple(d+3.14) // ok, but dangerous ``` ```c++ char a[10]; Y y; tuple(a, y); ``` -------------------------------- ### Include Boost.Tuple I/O Operators Source: https://www.boost.org/doc/libs/latest/libs/tuple/index To use input and output stream operators (like << and >>) with Boost.Tuple objects, include this header. It enables easy serialization and deserialization of tuples. ```cpp #include "boost/tuple/tuple_io.hpp" ``` -------------------------------- ### Tuple Streaming (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Shows how to stream tuples to and from `std::ostream` and `std::istream` using overloaded `operator<<` and `operator>>`. Includes manipulators like `set_open`, `set_close`, and `set_delimiter` for customizing output format. ```C++ tuple a(1.0f, 2, std::string("Howdy folks!"); // Default output cout << a; // Outputs: (1.0 2 Howdy folks!) // Customizing output cout << tuples::set_open('[') << tuples::set_close(']') << tuples::set_delimiter(',') << a; // Outputs: [1.0,2,Howdy folks!] // Input example tuple i; tuple j; cin >> i; // Reads data like (1 2 3) cin >> tuples::set_open('[') >> tuples::set_close(']') >> tuples::set_delimiter(':'); cin >> j; // Reads data like [4:5] ``` -------------------------------- ### Define Boost.Tuple Types Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Demonstrates the instantiation of `boost::tuple` template with different element types. The number of template parameters can range from 0 to 10 (or more if needed). Element types can be basic types, references, pointers, function pointers, and other template instantiations like `std::pair`. ```c++ tuple ``` ```c++ tuple ``` ```c++ tuple ``` ```c++ tuple > ``` ```c++ tuple, bool, void*> ``` -------------------------------- ### Create Tuples with make_tuple Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide The `make_tuple` function simplifies tuple creation by deducing element types. It can also handle explicit type control using `boost::ref` and `boost::cref` for references. ```C++ tuple add_multiply_divide(int a, int b) { return make_tuple(a+b, a*b, double(a)/double(b)); } A a; B b; const A ca = a; make_tuple(cref(a), b); // creates tuple make_tuple(ref(a), b); // creates tuple make_tuple(ref(a), cref(b)); // creates tuple make_tuple(cref(ca)); // creates tuple make_tuple(ref(ca)); // creates tuple make_tuple("Donald", "Daisy"); // creates tuple void f(int i); make_tuple(&f); // tuple ``` -------------------------------- ### Tuple vs. Hand-Made Tuple Performance (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Compares the performance of Boost.Tuple with a custom 'hand_made_tuple' class. Assumes existence of types A, B, and C. The code demonstrates construction and element access for both approaches, highlighting that optimizing compilers should yield similar performance. ```C++ class hand_made_tuple { A a; B b; C c; public: hand_made_tuple(const A& aa, const B& bb, const C& cc) : a(aa), b(bb), c(cc) {}; A& getA() { return a; }; B& getB() { return b; }; C& getC() { return c; }; }; hand_made_tuple hmt(A(), B(), C()); hmt.getA(); hmt.getB(); hmt.getC(); tuple t(A(), B(), C()); t.get<0>(); t.get<1>(); t.get<2>(); ``` -------------------------------- ### Include Boost.Tuple Comparison Operators Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Include this header to enable comparison operations (e.g., ==, !=, <, >) between Boost.Tuple objects. This header implicitly includes `boost/tuple/tuple.hpp`. ```c++ #include "boost/tuple/tuple_comparison.hpp" ``` -------------------------------- ### Tuple Unpacking with `tie` (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Shows how to use the `tie` function template in C++ to create a tuple of references, enabling 'unpacking' of another tuple's elements into existing variables. This is useful for assigning multiple return values from a function into separate variables. ```cpp int i; char c; double d; tie(i, c, d) = make_tuple(1, 'a', 5.5); std::cout << i << " " << c << " " << d; ``` -------------------------------- ### Include Boost.Tuple Headers Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Include the primary header for the Boost.Tuple library to access its core functionality. This header defines the `tuple` template and related utilities. ```c++ #include "boost/tuple/tuple.hpp" ``` -------------------------------- ### Include Boost.Tuple Comparison Operators Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Include this header to enable comparison operators (e.g., ==, !=, <, >) for Boost.Tuple objects. This allows for direct comparison of tuple contents. ```cpp #include "boost/tuple/tuple_comparison.hpp" ``` -------------------------------- ### Tuple Unpacking with tie() (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Illustrates how to use the `tie` function template to create tuples of references, enabling 'unpacking' of another tuple's elements into existing variables. This mechanism also works with `std::pair` and supports ignoring elements using `tuples::ignore`. ```C++ int i; char c; double d; tie(i, c, d) = make_tuple(1, 'a', 5.5); std::cout << i << " " << c << " " << d; // Output: 1 a 5.5 int i_pair; char c_pair; tie(i_pair, c_pair) = std::make_pair(1, 'a'); // Ignoring an element char c_ignore; tie(tuples::ignore, c_ignore) = std::make_pair(1, 'a'); ``` -------------------------------- ### Constructing Tuples with Reference Elements Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Shows how to construct tuples containing reference types. It highlights the requirement for explicit initialization of references and the distinction between initializing with non-const and const references, including dangers of dangling references. ```cpp double d = 5; tuple(d) // ok tuple(d+3.14) // ok, but dangerous ``` -------------------------------- ### Function Return Value Comparison - Direct Reference vs Tuple Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Compares two equivalent function signatures: one using non-const reference parameters for output and another returning a tuple. Demonstrates that reference-based returns (f1) may have slight performance advantage over tuple-based returns with tie assignment (f2) on some compilers. ```cpp void f1(int&, double&); tuple f2(); int i; double d; f1(i,d); // #1 tie(i,d) = f2(); // #2 ``` -------------------------------- ### Unpacking `std::pair` with `tie` (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Demonstrates the compatibility of `tie` with `std::pair` in C++. This allows elements of a `std::pair` to be unpacked into separate variables, similar to how it works with tuples. ```cpp int i; char c; tie(i, c) = std::make_pair(1, 'a'); ``` -------------------------------- ### Tuple Relational Operators (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Demonstrates the use of relational operators (==, !=, <, >, <=, >=) for comparing tuples. These operators work element-wise and support lexicographical ordering. Comparison is short-circuited and fails at compile time for tuples of different lengths. ```C++ tuple t1(std::string("same?"), 2, A()); tuple t2(std::string("same?"), 2, A()); tuple t3(std::string("different"), 3, A()); bool operator==(A, A) { std::cout << "All the same to me..."; return true; } t1 == t2; // true t1 == t3; // false, does not print "All the..." ``` -------------------------------- ### Tuple Lexicographical Ordering (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Illustrates the use of relational operators (<, >, <=, >=) for lexicographical ordering of tuples in C++. The comparison proceeds element by element from the first element until the result is determined. Comparing tuples of different lengths results in a compile-time error. ```cpp tuple t1("abc", 1); tuple t2("abd", 0); // t1 < t2 will evaluate to true because "abc" < "abd" ``` -------------------------------- ### Generic Tuple Element Manipulation with Cons Lists Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_advanced_interface Demonstrates generic functions for manipulating tuples represented as cons lists. The `set_to_zero` function recursively sets each element of a cons list to zero. It includes a base case for `null_type` and a recursive step for `cons` lists, highlighting the utility of treating tuples as cons lists for generic programming. ```cpp inline void set_to_zero(const null_type&) {} template inline void set_to_zero(cons& x) { x.get_head() = 0; // Assuming assignment is valid for H set_to_zero(x.get_tail()); } ``` -------------------------------- ### Streaming Tuples to `std::ostream` (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Details how tuples can be streamed to `std::ostream` in C++ using the overloaded `operator<<`. Elements are output recursively with a default space delimiter and enclosed in parentheses. Manipulators like `set_open`, `set_close`, and `set_delimiter` can customize the output format. ```cpp tuple a(1.0f, 2, std::string("Howdy folks!")); cout << a; // Output: (1.0 2 Howdy folks!) cout << tuples::set_open('[') << tuples::set_close(']') << tuples::set_delimiter(',') << a; // Output: [1.0,2,Howdy folks!] ``` -------------------------------- ### Tuple Copy Construction and Assignment Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Tuples support copy construction and assignment if their elements are respectively copy constructible or assignable. Conversions between compatible types are allowed during these operations. Assignment from std::pair is also supported. ```C++ class A {}; class B : public A {}; struct C { C(); C(const B&); }; struct D { operator C() const; }; tuple t; tuple a(t); // ok a = t; // ok tuple p = std::make_pair(1, 'a'); ``` -------------------------------- ### Tuple Copy Construction and Assignment (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/index Tuples support copy construction and assignment if their element types are element-wise copy constructible or assignable, respectively. Conversions between compatible types are allowed during these operations. Assignment is also defined from `std::pair` types. ```cpp class A {}; class B : public A {}; struct C { C(); C(const B&); }; struct D { operator C() const; }; tuple t; // Copy construction tuple a(t); // ok // Assignment a = t; // ok // Assignment from std::pair tuple pair_tuple = std::make_pair(1, 'a'); ``` -------------------------------- ### Hand-written Tuple Class Definition in C++ Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Defines a custom tuple-like class with three template parameters (A, B, C) and individual getter methods. This serves as a performance baseline for comparison with library-provided tuple implementations. The compiler can typically inline these methods to eliminate performance overhead. ```cpp class hand_made_tuple { A a; B b; C c; public: hand_made_tuple(const A& aa, const B& bb, const C& cc) : a(aa), b(bb), c(cc) {}; A& getA() { return a; }; B& getB() { return b; }; C& getC() { return c; }; }; hand_made_tuple hmt(A(), B(), C()); hmt.getA(); hmt.getB(); hmt.getC(); ``` -------------------------------- ### Ignoring Tuple Elements with `tuples::ignore` (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Explains the use of `tuples::ignore` to selectively ignore elements when unpacking a tuple or `std::pair` in C++. This is helpful when a function returns a tuple but you are only interested in a subset of its values. ```cpp char c; tie(tuples::ignore, c) = std::make_pair(1, 'a'); ``` -------------------------------- ### Tuple Equality and Inequality Comparison (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Demonstrates how Boost.Tuple overloads the equality (==) and inequality (!=) operators. These operators compare tuples element by element. The equality operator checks if all corresponding elements are equal, while the inequality operator checks if any pair of corresponding elements are not equal. Comparison stops early if the result is determined. ```cpp tuple t1(std::string("same?"), 2, A()); tuple t2(std::string("same?"), 2, A()); tuple t3(std::string("different"), 3, A()); bool operator==(A, A) { std::cout << "All the same to me..."; return true; } t1 == t2; // true t1 == t3; // false, does not print "All the..." ``` -------------------------------- ### Tuple Metafunctions: Element Type and Length Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_advanced_interface Provides metafunctions to query tuple types. `element::type` returns the type of the N-th element in tuple T, preserving constness. `length::value` returns the number of elements in tuple T. These are fundamental for compile-time introspection of tuple structures. ```cpp template struct element; template struct length; ``` -------------------------------- ### Streaming Tuples from `std::istream` (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_users_guide Describes how tuples can be extracted from `std::istream` using the overloaded `operator>>` in C++. This process recursively extracts elements using the stream's extraction operator. The manipulators `set_open`, `set_close`, and `set_delimiter` can be used to parse custom formats. Note that extracting tuples with string elements may not always be unambiguous. ```cpp tuple i; tuple j; cin >> i; // Reads input like "(1 2 3)" cin >> tuples::set_open('[') >> tuples::set_close(']') >> tuples::set_delimiter(':'); cin >> j; // Reads input like "[4:5]" ``` -------------------------------- ### Cons List Representation of Tuples Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_advanced_interface Illustrates the internal representation of Boost tuples as nested cons lists, terminating with `null_type`. This structure allows tuples to be manipulated using cons list operations. It defines `head_type`, `tail_type`, and provides access via `get_head()` and `get_tail()` member functions. ```cpp template struct cons { typedef A head_type; typedef B tail_type; A head; B tail; cons(const A& h, const B& t) : head(h), tail(t) {} A& get_head() { return head; } const A& get_head() const { return head; } B& get_tail() { return tail; } const B& get_tail() const { return tail; } }; struct null_type {}; // Example tuple inheritance: tuple inherits from cons>>>>; ``` -------------------------------- ### Tuple Internal Representation using Cons Lists (C++) Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/design_decisions_rationale Illustrates the internal representation of Boost tuples using a cons list structure. A tuple like `tuple` is shown to inherit from `cons>`, where `null_type` marks the end of the list. This representation is fundamental to how Boost handles tuple structures internally. ```cpp tuple inherits from cons > ``` -------------------------------- ### Tuple Element Type Computation: make_tuple_traits Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_advanced_interface Specifies the `make_tuple_traits` type function, which determines the element types for tuples created via `make_tuple`. It handles different types including references (compile-time error), array types (constant reference), `reference_wrapper` (reference), and regular types (as is), facilitating type deduction during tuple creation. ```cpp template struct make_tuple_traits { // Specializations handle different type mappings: // - references -> compile time error // - arrays -> const reference // - reference_wrapper -> U& // - other types -> T typedef T type; }; ``` -------------------------------- ### Tuple Element Type Traits: access_traits Source: https://www.boost.org/doc/libs/latest/libs/tuple/doc/html/tuple_advanced_interface Defines `access_traits` for determining how tuple elements are accessed and passed as parameters. It provides three type functions: `non_const_type` for non-const accessors, `const_type` for const accessors, and `parameter_type` for constructor arguments, ensuring correct type handling for various element types. ```cpp template struct access_traits { typedef T non_const_type; typedef const T const_type; typedef const T& parameter_type; // Example, actual type depends on T }; ```