C++0x: Type determination

From Dwarf Wiki
Jump to: navigation, search

For detail description of the feature, please refer to:

http://en.wikipedia.org/wiki/C%2B%2B0x#Type_inference

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2194.pdf

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2337.pdf

Overview

The definition of a variable with an explicit initialization can use the auto keyword. This creates a variable of the specific type of the initializer.

auto x = 7;                  // x has type int
template<class T> void printall(const vector<T>& v)
{
  for (auto p = v.begin();   // p has type vector<T>::const_iterator
       p!=v.end(); 
       ++p) 
    cout << *p << "\n";
}

decltype can be used to compile-time determine the type of an expression.

const std::vector<int> v(1);
decltype(v[0]) b;   // b has type const int&, the return type of 
                    // std::vector<int>::operator[](size_type)


Proposed change to DWARF

none.

auto and decltype are resolved to an actual type during compile time. DWARF type entries describes the actual type of the object.