Evaluation of type for auto in C++0X

Viewed 1208

I am playing around with the auto feature in the C++0X standard but I am confused how the decision of the type is made. Consider the following code.

struct Base
{
    virtual void f()
    {
        std::cout << "Base::f" << std::endl;
    }
};

struct Derived : public Base
{
    virtual void f()
    {
        std::cout << "Derived::f" << std::endl;
    }
};

int main()
{
    Base* dp = new Derived;
    auto b1 = *dp;
    auto& b2 = *dp;
    std::cout << typeid(b1).name() << std::endl;
    std::cout << typeid(b2).name() << std::endl;
}

It will print Base and Derived.
But why is the auto&evaluated to a ref to Derived and not to a ref to Base?
Even worse changing the code to this:

struct Base{};
struct Derived : public Base{};

int main()
{
    Base* dp = new Derived;
    auto b1 = *dp;
    auto& b2 = *dp;
    std::cout << typeid(b1).name() << std::endl;
    std::cout << typeid(b2).name() << std::endl;
}

returns Base for both types. So why is the type depended on the virtual functions? The compiler I am using is VS2010. Can anyone give me a hint where I can find the definition of this behavior in the standard?

5 Answers
Related