From: ManicQin on
Hello everybody.
In Scott Meyers lecture notes he states that one of the differences
between Auto and decltype is that the decltype does not evaluate the
expression.

I have a question regarding the evaluation of the expression, in the
next scenario what should I expect:

class B
{
public:
B()
{

}
virtual B* Clone()
{
cout << "B" << endl;
return new B();
}
};

class D : public B
{
public:
D(){}
virtual D* Clone()
{
cout << "D" << endl;
return new D();
}
};

int main()
{
B* tmp = new D();
auto test1 = tmp->Clone(); //returns D*!!!
decltype(tmp->Clone()) test2 = tmp->Clone();
return 0;
}

Please note That D::Clone overloads with a different return type.

In my understanding if the "auto" is evaluating so it means that the
type of test1 should be D*, but VS10 understands different :) what am
I missing?

thank you.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Uzdavinis on
On May 24, 10:45 am, ManicQin <manic...(a)gmail.com> wrote:

> I have a question regarding the evaluation of the expression, in the
> next scenario what should I expect:
>
> class B {
> public:
> B() { }
> virtual B* Clone() { return new B(); }
> };
>
> class D : public B {
> public:
> D() { }
> virtual D* Clone() { return new D(); }
>
> };
>
> int main() {
> B* tmp = new D();
> auto test1 = tmp->Clone(); //returns D*!!!
> decltype(tmp->Clone()) test2 = tmp->Clone();
> return 0;
>
> }
>
> Please note That D::Clone overloads with a different return type.

That's called a covariant return type.

> In my understanding if the "auto" is evaluating so it means that the
> type of test1 should be D*, but VS10 understands different :) what am
> I missing?

The type is determined at compile time, and the type of an expression
in C++ never changes. That is, the return type is NOT determined by
the
dynamic type of the return value, but of the static type at the point
where the call is made, at compile time.

Otherwise, imagine if it behaved what you seem to be wishing for:

// called with pointers to numerous types that all inherit from B...
void func(B * obj)
{
auto myClone = obj->Clone();
}

Ok, func is compiled *once*, and exists once in the application. For
auto to
declare an object of the dynamic type, that would require the body of
func()
to change depending on who called it.

That *could* work if func were a template, but it's not.

Maybe this makes it easier to see why the answer *has* to be the
static type (B*) and
not the dynamic type? If not, then consider one more thing.

Imagine your application has a plug-in interface, and 3rd parties can
provide
a shared library that your application will load. One function on
that shared library
is a factory function that produces objects that inherit from B.

extern B * generateObject();

When your application loads my library, it has NO idea what the real
type of object
I'll be returning, only its base class. To make the idea more
concrete, it's entirely
possibly your application is compiled, delivered, and installed,
before I ever even
started writing my library for it. So it's _impossible_ for your
application to know
anything about my code. Therefore, it should be clear that variables
in your code
do not (and cannot) refer to types in my code, without hiding behind
the polymorphic
interface.

Hope this helps.
Chris

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Peter C. Chapin on
ManicQin wrote:

> In my understanding if the "auto" is evaluating so it means that the
> type of test1 should be D*, but VS10 understands different :) what am
> I missing?

C++'s type system is static; all types are known to the compiler. In your case
the compiler sees the type B*. The type is not selected dynamically as would
be the case in a language like, for example, Python. However, when the
program runs, the expression is evaluated (for the case of auto).

Peter


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stuart Golodetz on
ManicQin wrote:
> Hello everybody.
> In Scott Meyers lecture notes he states that one of the differences
> between Auto and decltype is that the decltype does not evaluate the
> expression.
>
> I have a question regarding the evaluation of the expression, in the
> next scenario what should I expect:
>
> class B
> {
> public:
> B()
> {
>
> }
> virtual B* Clone()
> {
> cout << "B" << endl;
> return new B();
> }
> };
>
> class D : public B
> {
> public:
> D(){}
> virtual D* Clone()
> {
> cout << "D" << endl;
> return new D();
> }
> };
>
> int main()
> {
> B* tmp = new D();
> auto test1 = tmp->Clone(); //returns D*!!!
> decltype(tmp->Clone()) test2 = tmp->Clone();
> return 0;
> }
>
> Please note That D::Clone overloads with a different return type.
>
> In my understanding if the "auto" is evaluating so it means that the
> type of test1 should be D*, but VS10 understands different :) what am
> I missing?
>
> thank you.

I assume that what Scott means is that when you do

decltype(tmp->Clone()) test2 = tmp->Clone();

the tmp->Clone() in the decltype doesn't actually get evaluated (it's
only used for type deduction purposes). In the case of auto, there's no
expression similarly "associated with" the auto that could potentially
be evaluated in any case (it's just a stand-alone keyword). Note that
the expression on the right-hand side of the assignment involving auto
is evaluated at runtime, but then so is the tmp->Clone(); on the
right-hand side of the assignment involving decltype above - nothing
special about that.

The key point is that the compiler assigns types at *compile-time*
(since C++ is a statically-typed language). At that point, it has no
concept of "tmp points to a D" - all it deduces is that tmp->Clone() is
a call to B::Clone() since tmp is a B*. It therefore assigns test1 the
type returned by B::Clone(), namely B*. In other words, it uses the
static type of *tmp (B) when doing all of this, not the dynamic type
(D). The same is true in the test2 case.

Cheers,
Stu

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
ManicQin <manicqin(a)gmail.com> writes:

> Hello everybody.
> In Scott Meyers lecture notes he states that one of the differences
> between Auto and decltype is that the decltype does not evaluate the
> expression.
>
> I have a question regarding the evaluation of the expression, in the
> next scenario what should I expect:
>
> class B
> {
> public:
> B()
> {
>
> }
> virtual B* Clone()
> {
> cout << "B" << endl;
> return new B();
> }
> };
>
> class D : public B
> {
> public:
> D(){}
> virtual D* Clone()
> {
> cout << "D" << endl;
> return new D();
> }
> };
>
> int main()
> {
> B* tmp = new D();
> auto test1 = tmp->Clone(); //returns D*!!!
> decltype(tmp->Clone()) test2 = tmp->Clone();
> return 0;
> }
>
> Please note That D::Clone overloads with a different return type.
>
> In my understanding if the "auto" is evaluating so it means that the
> type of test1 should be D*, but VS10 understands different :) what am
> I missing?

To my understanding, the type of test2 will be B* owing to:

[dcl.type.simple]/4
"The type denoted by decltype(e) is defined as follows:
// ...
- otherwise, if e is a function call (5.2.2) or an invocation of an
overloaded operator (parentheses around e are ignored), decltype(e)
is the return type of the statically chosen function."

Now, when it comes to your application of the auto specifier,
effectively we need to apply a process corresponding template argument
deduction (see [dcl.spec.auto]/6) to follow it through, one which, IIUC,
requires that, for:

auto test1 = tmp->Clone();

"The type of [test1] is the deduced type of the parameter u in the
call f(expr) [for expr = tmp->Clone()] of the following invented
function template:

template <class U> void f(const U& u)"

Unfortunately, I am not yet able to find my way through the details
fully in the C++0x FCD, but applying this by way of example:

22:12:33 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $cat auto_decl.cpp
class B
{
public:
virtual B * Clone() { return new B(); }
};

class D : public B
{
public:
virtual D * Clone() { return new D(); }
};

template <class U> void f(const U& u) { }

int main()
{
B* b = new D();
f(b->Clone());
}

22:12:38 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $gcc -std=c++0x -c auto_decl.cpp

22:12:54 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $nm --demangle auto_decl.o | grep f\<
00000000 T void f<B*>(B* const&)

you'll see that this process of template argument deduction again
deduces according to the return type considered statically.

If this is the case, as it apparently it is, it would seem that the
notion of decltype(expr) providing an unevaluated context whereas auto
*does* `evaluate' in some sense (although I'm not quite sure of the exact
sense of Meyers' purported idea here) is not actually the factor that
decides it.

Having said this, again, a quote from Meyers would help here.

Regards

Paul Bibbings



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]