static_cast, cast operator, and constructors.

N

Noah Roberts

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?

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?
 
I

Ian Collins

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?

Is the code complete? The only option is the template.
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?

Specialise the template constructor for ObjectB.

template <>
ObjectA::ObjectA<ObjectB>(ObjectB const&)
{
std::cout << "special" << std::endl;
}
 
N

Noah Roberts

Is the code complete? The only option is the template.

No, there's a second option: apply the cast operator and call the copy
operator with the result.
 
I

Ian Collins

No, there's a second option: apply the cast operator and call the copy
operator with the result.

Your terminology is a little confused. There is a conversion (not cast)
operator and a copy constructor (not operator).

The sequence you describe required two levels of conversion, the
template constructor one.

Assignment or omitting the template constructor should call the
conversion operator:

ObjectA a = an_object_b;
 
J

Jorgen Grahn

....


Your terminology is a little confused. There is a conversion (not cast)
operator and a copy constructor (not operator).

And I personally get confused by a class called "ObjectA". It's a
class, not an object.

/Jorgen
 
J

James Kanze

Considder:
struct ObjectA
{
ObjectA(ObjectA const&);
template < typename T >
ObjectA(T const&);
void fun() const;
};

struct ObjectB
{
operator ObjectA () const;
};

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top