[c++ korea] effective modern c++ study item 3 understand decltype +이동우
TRANSCRIPT
Effective Modern C++ Study C++ Korea
Effective Modern C++ Study C++ Korea
template<typename typename >
void
???? = *
//...
Effective Modern C++ Study C++ Korea
Why do we need decltype?
template<typename typename >
void
auto = *
//...
Effective Modern C++ Study C++ Korea
Why do we need decltype?
template<typename typename >
void
// Pre-C++11 compiler extension, now obsolete
typedef typeof *
//...
Effective Modern C++ Study C++ Korea
Why do we need decltype?
template<typename typename >
void
typedef decltype *
Effective Modern C++ Study C++ Korea
Trailing return type
template<typename typename >
// Does not compile: lhs and rhs are not in scope
decltype(lhs * rhs)
return *
Effective Modern C++ Study C++ Korea
Trailing return type syntax
template<typename typename >
auto -> decltype(lhs * rhs)
return *
Effective Modern C++ Study C++ Korea
decltype(expr) : How to resolve the type
(expr == NAME*) ? AS DECLARED :
(expr == lvalue) ? T& :
// rvalue
(expr == xvalue) ? T&& : T //prvalue
* NAME: plain, unparenthesised variable, function - parameter, class member access
Effective Modern C++ Study C++ Korea
rvalue classification
* xvalue:
- function call where the function’s return value is declared as and rvalue reference
e.g. std:move(x)
- static cast to an rvalue reference
e.g. static_cast<A&&>(a)
- a member access of an xvalue
e.g. static_cast<A&&>(a)).m_x
* prvalue: all other rvalues than above cases
Effective Modern C++ Study C++ Korea
struct S { S() { m_x = 42; } int m_x; }; int x; const int cx = 42; const int& crx = x; const S* p = new S();
• decltype((x))
• decltype((cx))
• decltype((crx))
• decltype((p->m_x))
Effective Modern C++ Study C++ Korea
const S foo(); const int& foobar(); std::vector<int> vect = {42, 43};
• decltype(foo())
• decltype(fooobar())
• decltype(vect.begin())
• decltype(vect[0])
Effective Modern C++ Study C++ Korea
int x = 0; int y = 0; const int cx = 42; const int cy = 43; double d1 = 3.14; double d2 = 2.72;
* decltype(x * y) * decltype(cx * cy) * decltype(d1 > d2 ? d1 : d2) * decltype(x < d2 ? x : d2)
Effective Modern C++ Study C++ Korea
// C++11, deducing return type is available only for lambdas
template<typename Container, typename Index> auto authAndAccess(Container& c, Index i) -> decltype(c[i]) { authenticateUser(); return c[i]; }
// C++14, deducing is possible for lambdas and all functions
template<typename Container, typename Index> auto authAndAccess(Container& c, Index i) { authenticateUser(); return c[i]; } -> auto makes references in return type ignored
Effective Modern C++ Study C++ Korea
// C++14
template<typename Container, typename Index>
decltype(auto)
authAndAccess(Container& c, Index i)
{
authenticateUser();
return c[i];
}
-> decltype(auto)follows decltype rule as Scott Meyer explained. Now return value can be produced as a reference
Effective Modern C++ Study C++ Korea
std::deque<std::string> makeStringDeque();
auto s = authAndAccess(makeStringDeque(), 5);
// C++14
template<typename Container, typename Index>
decltype(auto)
authAndAccess(Container&& c, Index i)
{
authenticateUser();
return std::forward<Container>(c)[i];
}
-> rvalue reference can be dealt with.
Effective Modern C++ Study C++ Korea
std::deque<std::string> makeStringDeque();
auto s = authAndAccess(makeStringDeque(), 5);
// C++11
template<typename Container, typename Index>
auto
authAndAccess(Container&& c, Index i)
-> decltype(std::forward<Container>(c)[i])
{
authenticateUser();
return std::forward<Container>(c)[i];
}
-> rvalue reference can be dealt with.
Effective Modern C++ Study C++ Korea
- decltype almost always yields the type of a variable or expression without any modifications.
- For lvalue expressions of type T other than names, decltype always reports a type of T&.
- C++14 supports decltype(auto), which, like auto, deduces a type from its initializer, but it performs the type deduction using the decltype rules.
Effective Modern C++ Study C++ Korea
• http://thbecker.net/articles/auto_and_decltype/section_05.html
• http://scottmeyers.blogspot.kr/2013/07/ when-decltype-meets-auto.html