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

FreeBSD Manual Pages

  
 
  

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

NAME
       std::any::any - std::any::any

Synopsis
	  constexpr			  any()			     noexcept;
       (1) (since C++17)
	  any(		  const		   any&		   other	    );
       (2) (since C++17)
	  any(		 any&&		 other		 )	     noexcept;
       (3) (since C++17)
	  template<		 class		     ValueType		     >
       (4) (since C++17)
	  any( ValueType&& value );
	  template<	  class	     ValueType,	     class...	   Args	     >
       (5) (since C++17)
	  explicit any(	std::in_place_type_t<ValueType>, Args&&... args	);
	  template< class ValueType, class U, class... Args >

	  explicit	      any(	      std::in_place_type_t<ValueType>,
       (6) (since C++17)
	  std::initializer_list<U> il,

	  Args&&... args );

	  Constructs a new any object.

	  1) Constructs	an empty object.
	  2-3)	Copies	(2) or moves (3) content of other into a new instance,
       so that any
	  content is equivalent	in both	type and value to those	of other prior
       to the
	  constructor call, or empty if	other is empty.	Formally,
	  2) If	other is empty,	the constructed	object	is  empty.  Otherwise,
       equivalent to
	  any(std::in_place_type<T>,  std::any_cast<const T&>(other)), where T
       is the type of
	  the object contained in other.
	  3) If	other is empty,	the constructed	object	is  empty.  Otherwise,
       the constructed
	  object  contains  either the object contained	in other, or an	object
       of the same type
	  constructed from the object contained	in other, considering that ob-
       ject as an

rvalue.
	  4) Constructs	an object with initial content an object of type
	  std::decay_t<ValueType>, direct-initialized from std::forward<Value-
       Type>(value).

	    * This overload participates in overload resolution	only if
	      std::decay_t<ValueType> is not the same type as any nor  a  spe-
       cialization of
	      std::in_place_type_t,  and std::is_copy_constructible_v<std::de-
       cay_t<ValueType>>
	      is true.

	  5) Constructs	an object with initial content an object of type
	  std::decay_t<ValueType>, direct-non-list-initialized from
	  std::forward<Args>(args)....

	    * This overload participates in overload resolution	only if
	      std::is_constructible_v<std::decay_t<ValueType>, Args...>	and
	      std::is_copy_constructible_v<std::decay_t<ValueType>>  are  both
       true.

	  6) Constructs	an object with initial content an object of type
	  std::decay_t<ValueType>, direct-non-list-initialized from il,
	  std::forward<Args>(args)....

	    * This overload participates in overload resolution	only if
	      std::is_constructible_v<std::decay_t<ValueType>,	 std::initial-
       izer_list<U>&,
	      Args...>	and   std::is_copy_constructible_v<std::decay_t<Value-
       Type>> are both
	      true.

Template parameters
	  ValueType		  -		 contained value type

Type requirements
	  -
	  std::decay_t<ValueType>  must	 meet  the  requirements  of  CopyCon-
       structible.

Parameters
	  other	   - another any object	to copy	or move	from
	  value	   - value to initialize the contained value with
	  il, args - arguments to be passed to the  constructor	 of  the  con-
       tained object

Exceptions
	  2,4,5,6)  Throws any exception thrown	by the constructor of the con-
       tained type.

Notes
	  Because the default constructor is constexpr,	static	std::anys  are
       initialized as
	  part	of static non-local initialization, before any dynamic non-lo-
       cal initialization
	  begins. This makes it	safe to	use an object of type  std::any	 in  a
       constructor of any
	  static object.

Example
       // Run this code

	#include <utility>
	#include <set>
	#include <any>
	#include <string>
	#include <memory>
	#include <iostream>
	#include <initializer_list>
	#include <cxxabi.h>

	struct A {
	    int	age;
	    std::string	name;
	    double salary;
	};

	// using abi demangle to print nice type name of instance of any hold-
       ing
	void printType(const std::any& a) {
	    int	status;
	    if	(char*	p  =  abi::__cxa_demangle(a.type().name(), 0, 0, &sta-
       tus)){
		std::cout << p << '\n';
		std::free(p);
	    }
	}

	int main(){
	    // constructor #4: std::any	holding	int
	    std::any a1{7};

	    // constructor #5: std::any	holding	A, constructed in place
	    std::any a2(std::in_place_type_t<A>{}, 30, "Ada", 1000.25);

	    // constructor #6: std::any	holding	a set of A with	custom compar-
       ison
	    auto lambda	= [](auto&& l, auto&& r){ return l.age < r.age;	};
	    std::any a3(
		std::in_place_type_t<std::set<A, decltype(lambda)>>{},
		std::initializer_list<A>{
		    {39, std::string{"Ada"}, 100.25},
		    {20, std::string{"Bob"}, 75.5}
		},
		lambda);

	    printType(a1);
	    printType(a2);
	    printType(a3);
	}

Possible output:
	int
	A
	std::set<A, main::{lambda(auto:1&&, auto:2&&)#1}, std::allocator<A> >

See also
	  operator= assigns an any object
		    (public member function)

http://cppreference.com		  2022.07.31		      std::any::any(3)

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

home | help