By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,405 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,405 IT Pros & Developers. It's quick & easy.

static_cast vs. C-stype cast

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:3f******@news.uni-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
Jul 22 '05 #2

P: n/a
Alexander Stippler <st**@mathematik.uni-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

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.