Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static_cast, cast operator, and constructors.

Reply
Thread Tools

static_cast, cast operator, and constructors.

 
 
Noah Roberts
Guest
Posts: n/a
 
      07-18-2011
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-18-2011
On 07/19/11 10:46 AM, Noah Roberts 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?


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;
}

--
Ian Collins
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      07-18-2011
On 7/18/2011 4:29 PM, Ian Collins wrote:
> On 07/19/11 10:46 AM, Noah Roberts 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?

>
> 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.

>
>> 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;
> }
>


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-18-2011
On 07/19/11 11:44 AM, Noah Roberts wrote:
> On 7/18/2011 4:29 PM, Ian Collins wrote:
>> On 07/19/11 10:46 AM, Noah Roberts 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?

>>
>> 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.


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;

--
Ian Collins
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-19-2011
On Mon, 2011-07-18, Ian Collins wrote:
> On 07/19/11 11:44 AM, Noah Roberts wrote:
>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>> Considder:
>>>>
>>>> struct ObjectA
>>>> {

....

>> 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).


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

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      07-19-2011
On 7/19/2011 6:12 AM, Jorgen Grahn wrote:
> On Mon, 2011-07-18, Ian Collins wrote:
>> On 07/19/11 11:44 AM, Noah Roberts wrote:
>>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>>> Considder:
>>>>>
>>>>> struct ObjectA
>>>>> {

> ...
>
>>> 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).

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


You guys get worked up over some pretty weird ****.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-20-2011
On Tue, 2011-07-19, Noah Roberts wrote:
> On 7/19/2011 6:12 AM, Jorgen Grahn wrote:
>> On Mon, 2011-07-18, Ian Collins wrote:
>>> On 07/19/11 11:44 AM, Noah Roberts wrote:
>>>> On 7/18/2011 4:29 PM, Ian Collins wrote:
>>>>> On 07/19/11 10:46 AM, Noah Roberts wrote:
>>>>>> Considder:
>>>>>>
>>>>>> struct ObjectA
>>>>>> {

>> ...
>>
>>>> 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).

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

>
> You guys get worked up over some pretty weird ****.


I wasn't worked up. But surely you want your postings to be as clear as
possible?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2011
On Jul 18, 11:46 pm, Noah Roberts <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
Quartus VHDL problem with aggregate and type cast rickman VHDL 3 07-15-2003 04:24 AM



Advertisments