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

FreeBSD Manual Pages

  
 
  

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

NAME
       std::declval - std::declval

Synopsis
	  Defined in header <utility>
	  template<class						    T>
       (since C++11)
	  typename std::add_rvalue_reference<T>::type declval()	noexcept;

	  Converts any type T to a reference type, making it possible  to  use
       member functions
	  in decltype expressions without the need to go through constructors.

	  declval  is commonly used in templates where acceptable template pa-
       rameters	may have
	  no constructor in common, but	have the same  member  function	 whose
       return type is
	  needed.

	  Note	that  declval  can only	be used	in unevaluated contexts	and is
       not required to be
	  defined; it is an error to evaluate an expression that contains this
       function.
	  Formally, the	program	is ill-formed if this function is odr-used.

Parameters
	  (none)

Return value
	  Cannot be called and thus never returns a value. The return type  is
       T&& unless T is
	  (possibly cv-qualified) void,	in which case the return type is T.

Possible implementation
	  template<typename T> constexpr bool always_false = false;

	  template<typename T>
	  typename std::add_rvalue_reference<T>::type declval()	noexcept {
	      static_assert(always_false<T>, "declval not allowed in an	evalu-
       ated context");
	  }

Example
       // Run this code

	#include <utility>
	#include <iostream>

	struct Default { int foo() const { return 1; } };

	struct NonDefault
	{
	    NonDefault() = delete;
	    int	foo() const { return 1;	}
	};

	int main()
	{
	    decltype(Default().foo())  n1 = 1;			 // type of n1
       is int
	//  decltype(NonDefault().foo()) n2 = n1;		//  error:  no
       default constructor
	    decltype(std::declval<NonDefault>().foo())	n2 = n1; // type of n2
       is int
	    std::cout << "n1 = " << n1 << '\n'
		      << "n2 = " << n2 << '\n';
	}

Output:
	n1 = 1
	n2 = 1

See also
	  decltype specifier(C++11) obtains the	type of	an  expression	or  an
       entity
	  result_of		     deduces  the  result  type	 of invoking a
       callable	object with
	  invoke_result		    a set of arguments
	  (C++11)(removed in C++20) (class template)
	  (C++17)

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

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

home | help