-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclval.cpp
More file actions
29 lines (25 loc) · 1.07 KB
/
Copy pathdeclval.cpp
File metadata and controls
29 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <utility>
#include <iostream>
/*
* 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 parameters 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.
*
*/
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';
}