Re: decltype and const reference params
Noah Roberts <noneed@toemailme.com> writes:
<snip />
In article <87eiirxjw4.fsf@gmail.com>, paul.bibbings@gmail.com says...
... can I ask what compiler you are using?
VS 2010 RC
Having only just obtained the C++0x FCD I'm not that clued up on what
the correct interpretation of the return type of your op+ should be, not
having much reading time at the moment. Still, I would be very suprised
to see how:
lh = const point<int>&
rh = const point<double>&
could lead to:
decltype(lh.x + rh.x) => const double
and not just double.
Have you checked yourself that what irritates you here is expected and
not simply incorrect? What error messages are you getting from your
compiler?
1>e:\dev_workspace\experimental\2010_feature_assessment\2010
_feature_assessment\main.cpp(148): error C2440: 'initializing' : cannot
convert from 'point<T>' to 'point<T>'
1> with
1> [
1> T=const double
1> ]
1> and
1> [
1> T=double
1> ]
1> No constructor could take the source type, or constructor
overload resolution was ambiguous
line 148 is "point<double> z = x + y;"
To pursue this further, since we have the case of two implementations
disagreeing on how your code should behave, I have now had a quick
browse through the C++0x FCD and found this example, which is not
a direct counterpart to your example as is, however:
// ...
struct A { double x; };
const A* a = new A();
// ...
decltype(a->x) x3; // type is double
decltype((a->x)) x4 = x3; // type is const double&
This example is repeated on the MSDN at
http://msdn.microsoft.com/en-us/library/dd537655(VS.100).aspx.
Here, A is not a template, so a first point of difference depends on
whether the above example would lead to the same types if we replaced:
template<typename T>
struct A { T x; };
const A<double>* a = new A<double>();
A second difference, of course, is that this example uses a pointer to a
const A and not a reference. Accepting these differences, this example
indicates that the type of decltype(a->x) does not take on the
cv-qualification if it's enclosing type, so to speak. That is, *a is
const A, but that doesn't make decltype(a->x) into const double. I
might therefore expect a similar handling of decltype(lh.x + rh.x) in
your code (see OP) for:
template< typename T1, typename T2 >
auto operator + (point<T1> const& lh, point<T2> const& rh)
-> point< decltype(lh.x + rh.x);
I am of course, as ever, open to be shown the flaws in my thinking.
What's more puzzling to me, however, is why Comeau rejects your code
altogether with:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for
ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 11: error: "auto" is not allowed here
auto operator + (point<T1> const& lh, point<T2> const& rh)
^
"ComeauTest.c", line 12: error: expected a ";" (perhaps on the
previous statement)
-> point< decltype(lh.x + rh.x) >
^
At end of source: warning: parsing restarts here after previous syntax
error
2 errors detected in the compilation of "ComeauTest.c".
Regards
Paul Bibbings