Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static_cast vs. C-stype cast

Reply
Thread Tools

static_cast vs. C-stype cast

 
 
Alexander Stippler
Guest
Posts: n/a
 
      12-08-2003
In some situations I can apply a C-style cast,
but no static_cast. I'm not quite sure about the
differences in such places. A little example:

template <typename T>
class Dummy {
public:
Dummy(int i) {}
};

template <typename T>
void dummy(const Dummy<T> &a) {}

int
main()
{
int i=6;
// a traditional cast works ...
dummy((const Dummy<int> &)i);

// but the static_cast is an invalid type conversion.
dummy(static_cast<const Dummy<int> &>(i));
return 0;
}


I know, that a static_cast to Dummy<int> would have worked in the second
call, but what's the difference between the two casts in the main() above?

regards,
alex
 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      12-09-2003

"Alexander Stippler" <(E-Mail Removed)-ulm.de> wrote in message
news:(E-Mail Removed)-ulm.de...

[snip]

| int
| main()
| {
| int i=6;
| // a traditional cast works ...
| dummy((const Dummy<int> &)i);
|
| // but the static_cast is an invalid type conversion.
| dummy(static_cast<const Dummy<int> &>(i));
| return 0;
| }
|
|
| I know, that a static_cast to Dummy<int> would have worked in the second
| call, but what's the difference between the two casts in the main() above?

Are you sure you didn't want the following instead ?:

dummy( ( const Dummy<int> ) i );
dummy( static_cast<const Dummy<int> >( i ) );

Cheers.
Chris Val


 
Reply With Quote
 
 
 
 
Hendrik Schober
Guest
Posts: n/a
 
      12-10-2003
Alexander Stippler <(E-Mail Removed)-ulm.de> wrote:
> In some situations I can apply a C-style cast,
> but no static_cast. I'm not quite sure about the
> differences in such places. A little example:
>
> template <typename T>
> class Dummy {
> public:
> Dummy(int i) {}
> };
>
> template <typename T>
> void dummy(const Dummy<T> &a) {}
>
> int
> main()
> {
> int i=6;
> // a traditional cast works ...
> dummy((const Dummy<int> &)i);
>
> // but the static_cast is an invalid type conversion.
> dummy(static_cast<const Dummy<int> &>(i));
> return 0;
> }
>
>
> I know, that a static_cast to Dummy<int> would have worked in the second
> call, but what's the difference between the two casts in the main() above?


I'm not sure. Maybe it's considered to be
two casts: 'int' ==> 'Dummy' and then
casting the temporary to a const reference?
BTW, what's so bad about
dummy( Dummy<int>(i) )
? I think this
dummy<int>( i );

should work as well.

> regards,
> alex



Schobi

--
http://www.velocityreviews.com/forums/(E-Mail Removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers


 
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
static_cast, cast operator, and constructors. Noah Roberts C++ 7 07-20-2011 07:53 PM
static_cast better than C cast ? Vincent RICHOMME C++ 9 05-03-2007 07:44 AM
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
Moving to static_cast and reinterpret_cast from old C-style cast Kobe C++ 3 02-15-2006 08:13 PM
static_cast versus C-style type cast Dave Rahardja C++ 1 08-18-2003 08:34 AM



Advertisments