473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

static_cast vs. C-stype cast

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
2 2113

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wenjie | last post by:
Hello, I have a class B to mediate/isolate between implementation and their users (like used in an abstract factory class pattern): // B.h class B { static B* getImpl(); static void...
4
by: Otto Lind | last post by:
The following program shows the problem: #include <stdio.h> namespace x { enum L { One = 1, Two = 2 }; namespace x { enum L { Three = 3, Four = 4 };
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
6
by: bonham | last post by:
i m a beginner in C++. I have some questions: what is static_cast<unsigned>? i saw someone use static_cast<unsigned> and static_cast<unsigned char> together. why? e.g....
6
by: Dave Rahardja | last post by:
Is it safe to use static_cast to downcast from a virtual base class? For example, class V {}; class A: public virtual V {}; class B: public virtual V {}; class C: public A, public B; void...
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
1
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post by:
In C++ we can not overload the explicit typecast operator like static_cast, dynamic_cast, const_cast and reinterpret_cast. The only we can do is the implicit typecast operator like operator...
3
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.