# std::declval

> C++ Atlas: `cppatlas.com/w/cpp/utility/declval` | Source: https://en.cppreference.com/w/cpp/utility/declval | Domain: Utility | Standards: C++11, C++14, C++17, C++20

Helper template for writing expressions that appear in unevaluated contexts, typically the operand of decltype.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <utility>

template< class T >
typename std::add_rvalue_reference<T>::type declval() noexcept; // (since C++11) (until C++14) (unevaluated-only)

template< class T >
std::add_rvalue_reference_t<T> declval() noexcept; // (since C++14) (unevaluated-only)
```

Helper template for writing expressions that appear in unevaluated contexts, typically the operand of `decltype`. In unevaluated context, this helper template converts any type `T` (which may be an incomplete type) to an expression of that type, making it possible to use member functions of T without the need to go through constructors.

`std::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 evaluated and thus never returns a value. The return type is `T&&` (reference collapsing rules apply) unless `T` is (possibly cv-qualified) `void`, in which case the return type is `T`.

### Notes

`std::declval` is commonly used in templates where acceptable template parameters may have no constructor in common, but have the same member function whose return type is needed.

### Possible implementation

```cpp
template<typename T>
typename std::add_rvalue_reference<T>::type declval() noexcept
{
    static_assert(false, "declval not allowed in an evaluated context");
}
```

### Example

```cpp
#include <iostream>
#include <utility>

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(std::declval<Default>().foo()) n2 = 1;     // same

//  decltype(NonDefault().foo())               n3 = n1; // error: no default constructor
    decltype(std::declval<NonDefault>().foo()) n3 = n1; // type of n3 is int

    std::cout << "n1 = " << n1 << '\n'
              << "n2 = " << n2 << '\n'
              << "n3 = " << n3 << '\n';
}
```

Output:

```text
n1 = 1
n2 = 1
n3 = 1
```

### See also

- [`decltype` specifier (C++11)](https://en.cppreference.com/w/cpp/language/decltype): obtains the type of an expression or an entity
- [`result_of`](https://en.cppreference.com/w/cpp/types/result_of) (C++11), [`invoke_result`](https://en.cppreference.com/w/cpp/types/result_of) (removed in C++20): deduces the result type of invoking a callable object with a set of arguments (class template)

---
Attribution: content from https://en.cppreference.com/w/cpp/utility/declval (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.