Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/declval"

From cppreference.com
< cpp‎ | utility
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
(Added explanation of how it works)
Line 9: Line 9:
 
{{ddcl list end}}
 
{{ddcl list end}}
  
Converts any type {{tt|T}} to a reference type, making it possible to use member functions in {{c|decltype}} expressions without specifying constructors. It 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. {{c|std::declval}} can only be used in unevaluated contexts, it is an error to evaluate an expression that contains this function.
+
Converts any type {{tt|T}} to a reference type, making it possible to use member functions in {{c|decltype}} expressions without specifying constructors. It 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. Note that, no definition exists for {{c|std::declval}} function. So, {{c|std::declval}} can only be used in unevaluated contexts, it is an error to evaluate an expression that contains this function.  
  
 
===Parameters===
 
===Parameters===
Line 19: Line 19:
 
===Exceptions===
 
===Exceptions===
 
{{noexcept}}
 
{{noexcept}}
 +
 +
===Explanation===
 +
Since declvalue converts the function type to rvalue reference, compiler will not evaluate it. 
  
 
===Example===
 
===Example===

Revision as of 22:01, 29 January 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <utility>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

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

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Converts any type T to a reference type, making it possible to use member functions in decltype expressions without specifying constructors. It 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. Note that, no definition exists for std::declval function. So, std::declval can only be used in unevaluated contexts, it is an error to evaluate an expression that contains this function.

Contents

Parameters

(none)

Return value

Cannot be called, thus never returns a value, but the return type is T&& unless T is an lvalue reference type, in which case T& is returned.

Exceptions

noexcept specification:  
noexcept
  

Explanation

Since declvalue converts the function type to rvalue reference, compiler will not evaluate it.

Example

#include <utility>
#include <iostream>
 
struct Default {
    int foo() const {return 1;}
};
 
struct NonDefault {
    NonDefault(const NonDefault&) {}
    int foo() const {return 1;}
};
 
int main()
{
    decltype(Default().foo()) n1 = 1; // int n1
//  decltype(NonDefault().foo()) n2 = n1; // will not compile
    decltype(std::declval<NonDefault>().foo()) n2 = n1; // int n2
    std::cout << "n2 = " << n2 << '\n';
}

Output:

n2 = 1

See also

Template:cpp/language/dcl list decltype
(C++11)(removed in C++20)(C++17)
deduces the result type of invoking a callable object with a set of arguments
(class template) [edit]