Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::tie(3)		      C++ Standard Libary		   std::tie(3)

NAME
       std::tie	- std::tie

Synopsis
	  Defined in header <tuple>
	  template< class... Types >				 (since	C++11)
	  std::tuple<Types&...>	 tie(  Types&...  args ) noexcept;  (constexpr
       since C++14)

	  Creates a tuple of lvalue references to its arguments	 or  instances
       of std::ignore.

Parameters
	  args - zero or more lvalue arguments to construct the	tuple from.

Return value
	  A std::tuple object containing lvalue	references.

Possible implementation
	  template <typename...	Args>
	  constexpr // since C++14
	  std::tuple<Args&...> tie(Args&... args) noexcept
	  {
	      return {args...};
	  }

Notes
	  std::tie  may	be used	to unpack a std::pair because std::tuple has a
       converting
	  assignment from pairs:

	bool result;
	std::tie(std::ignore, result) =	set.insert(value);

Example
	  1) std::tie can be used to introduce lexicographical comparison to a
       struct or to
	  unpack a tuple;
	  2) std::tie can work with structured bindings:

       // Run this code

	#include <cassert>
	#include <iostream>
	#include <set>
	#include <string>
	#include <tuple>

	struct S
	{
	    int	n;
	    std::string	s;
	    float d;

	    friend bool	operator<(const	S& lhs,	const S& rhs) noexcept
	    {
		// compares lhs.n to rhs.n,
		// then	lhs.s to rhs.s,
		// then	lhs.d to rhs.d
		// in that order, first	non-equal result is returned
		// or false if all elements are	equal
		return std::tie(lhs.n, lhs.s, lhs.d) < std::tie(rhs.n,	rhs.s,
       rhs.d);
	    }
	};

	int main()
	{
	    // Lexicographical comparison demo:
	    std::set<S>	set_of_s;

	    S value{42,	"Test",	3.14};
	    std::set<S>::iterator iter;
	    bool is_inserted;

	    // Unpack a	pair:
	    std::tie(iter, is_inserted)	= set_of_s.insert(value);
	    assert(is_inserted);

	    // std::tie	and structured bindings:
	    auto position = [](int w) {	return std::tuple(1 * w, 2 * w); };

	    auto [x, y]	= position(1);
	    assert(x ==	1 && y == 2);
	    std::tie(x,	y) = position(2); // reuse x, y	with tie
	    assert(x ==	2 && y == 4);

	    // Implicit	conversions are	permitted:
	    std::tuple<char, short> coordinates(6, 9);
	    std::tie(x,	y) = coordinates;
	    assert(x ==	6 && y == 9);
	}

See also
	  Structured  binding (C++17) binds the	specified names	to sub-objects
       or tuple
				     elements of the initializer
	  make_tuple		     creates a tuple object of	the  type  de-
       fined by	the
	  (C++11)		     argument types
				     (function template)
	  forward_as_tuple	     creates a tuple of	forwarding references
	  (C++11)		     (function template)
	  tuple_cat		     creates a tuple by	concatenating any num-
       ber of tuples
	  (C++11)		     (function template)
	  ignore		      placeholder  to skip an element when un-
       packing a tuple
	  (C++11)		     using tie
				     (constant)

http://cppreference.com		  2024.06.10			   std::tie(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::tie&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help