Thursday 23 October 2014

C++ decltype and auto type deduction

auto type deduction strips const, volatile and ref

const int& bar = foo;
auto baz = bar; // strips const and ref - therefore type of baz is int

decltype type deduction doesn't strip const, volatile and ref

// decltype of a name

const int& bar = foo;
decltype(bar) // does not strip const and ref - therefore type is const int&

// decltype of an expression
decltype(lvalue expression) always returns an lvalue reference

int arr[5];
arr[0] = 5;
decltype(arr[0]) // lvalue reference, therefore type is int&



No comments:

Post a Comment