An important property of decltype is that its operand
never gets evaluated. For example, you can use an out-of-bounds
element access to a vector as the operand of decltype
with impunity:
std::vector<int> vect;
assert(vect.empty());
typedef decltype(vect[0]) integer;
Another property of decltype that is worth pointing out
is that when decltype(expr) is the name of a
plain user defined type (not a reference or pointer, not a basic or function type),
then decltype(expr) is also a class name. This means that you
can access nested types directly:
template<typename R>
class SomeFunctor {
public:
typedef R result_type;
result_type operator()() {
return R();
}
SomeFunctor(){}
};
SomeFunctor<int> func;
typedef decltype(func)::result_type integer; // access nested type
You can even use decltype(expr) to specify a base class:
auto foo = [](){return 42;};
class DerivedFunctor : public decltype(foo)
{
public:
MyFunctor(): decltype(foo)(foo) {}
// ...
};
|