A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/utility/declval/ below:

function template

<utility>

std::declval
template <class T>  typename add_rvalue_reference<T>::type declval() noexcept;

Declaration value

Returns an rvalue reference to type T without referring to any object.

This function shall only be used in unevaluated operands (such as the operands of sizeof and decltype).

T may be an incomplete type.

This is a helper function used to refer to members of a class in unevaluated operands, especially when either the constructor signature is unknown or when no objects of that type can be constructed (such as for abstract base classes).



Parameters none

Return value An rvalue reference to type T.
The return type uses add_rvalue_reference to return T&& when T is an object type.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// declval example
#include <utility>      // std::declval
#include <iostream>     // std::cout

struct A {              // abstract class
  virtual int value() = 0;
};

class B : public A {    // class with specific constructor
  int val_;
public:
  B(int i,int j):val_(i*j){}
  int value() {return val_;}
};

int main() {
  decltype(std::declval<A>().value()) a;  // int a
  decltype(std::declval<B>().value()) b;  // int b
  decltype(B(0,0).value()) c;   // same as above (known constructor)
  a = b = B(10,2).value();
  std::cout << a << '\n';
  return 0;
}

Output:


Data races Calling this function introduces no data races.

ExceptionsNo-throw guarantee: this function never throws exceptions.

See also
common_type
Common type (class template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4