Re: static_cast, cast operator, and constructors.
On Jul 18, 11:46 pm, Noah Roberts <d...@email.me> wrote:
Considder:
struct ObjectA
{
ObjectA(ObjectA const&);
template < typename T >
ObjectA(T const&);
void fun() const;
};
struct ObjectB
{
operator ObjectA () const;
};
static_cast<ObjectA>(an_object_b).fun();
Which constructor does the standard specify will be called during the
static_cast: copy or template?
My first reaction is that it's ambiguous. In both cases
(supposing "an_object_b" has type ObjectB), you need a const
conversion, followed by a user defined conversion. There is
a rule that all other things being equal, a non-template will
have precedence over a template, but I'm not sure it applies
here. And there are all sorts of subtilities involving overload
resolution in the presence of const; I don't have a copy of the
standard here to try to work them out.
Behavior I am seeing is that the templated version is called and then
bases are called on the result of the cast operator.
Is there a way to explicitly call the cast operator or is that what I'm
supposedly doing?
There's no way of explicitly calling either the conversion
operator or the constructor; the only way either gets called is
because of a conversion (explicit or implicit). My feeling is
that either the conversion is ambiguous, or perhaps the
conversion operator should be called, because of the
non-template over template rule; but without having a copy of
the standard to verify it by, I wouldn't want to swear on it.
--
James Kanze