3

I have heard that static cast is a safer way to do casting.

Lets say that I have the following code:

int nValue = 48; char ch = nValue; 

This is implicit casting. But it is unsafe to change 4 bytes to 1 byte. If I will change this code to this:

int nValue = 48; char ch = static_cast<char>(nValue); 

It would be safer. why? what makes it safer? It is still changing 4 bytes to 1 byte.

6
  • 2
    It's not any safer to the computer but it's more explicit to anyone reading the source-code. Commented May 5, 2014 at 15:53
  • 4
    explicit casting is there to tell the compiler you know what you're doing. The compiler won't warn you because if you did it explicitly - its safe to assume you know the risk. I'm not sure if thats what you're asking, so I'll just leave a comment. Commented May 5, 2014 at 15:53
  • It's useful if you examine what "safer" means. Since you "heard it somewhere" we don't really know what that word means to you. Commented May 5, 2014 at 15:55
  • 1
    If the casting is unsafe, then a standard cast will go without a warning, while a static_cast will issue a compilation error. The unsafety-ness is not visible in your example, but consider two different classes A and B (not related through inheritance or anything). Now, consider two pointers A* a and B* b. A standard cast such as b = (B*)a would go without a warning, although b would be very unsafe to use. A static_cast will yield a compilation error, preventing you from running your program until you've fixed it. Commented May 5, 2014 at 15:58
  • 2
    @Deduplicator The first example is perfectly valid. Narrowing implicit conversions are only an error in C++11 inside curly braces. Prior to C++11, and in C++11 outside of curly braces, they're allowed. Commented May 5, 2014 at 16:01

2 Answers 2

13

A static_cast is not safer than implicit conversion. However, you might have read that a static_cast is safer than a C cast. That's because it only allows "reasonable" casts. Some examples of a reasonable cast would be between numeric types, void * to some other pointer, base class ptr to derived class ptr. For example:

class Base {}; class D1 : public Base {}; class D11 {}; ... Base *ptr_to_Base(...); // Get from somewhere. D11 *p = (D11 *) ptr_to_Base; // No compiler error! I need new eyeglasses! D11 *p2 = static_cast<D11 *>(ptr_to_Base); // This gives you a compiler error. 

Note that a static_cast can still allow bad casts, just not "unreasonable" ones:

class D2 : public Base {}; ... Base *b = new D2; D1 *p3 = static_cast<D1 *>(b); // Wrong, but allowed by compiler. 
Sign up to request clarification or add additional context in comments.

5 Comments

You may show what is ptr_to_Base. And also avoid to mix D1 and Dl.
@Jarod42: I'll edit to add ptr_to_Base definition. But I intentionally used two names that are easy to confuse, to help motivate the usage.
I agree with Jarod42. It took me a couple of minutes to figure out how the pointer declarations were different. The l and 1 are nearly identical that I couldn't figure out what you are trying to say.
@shawn1874: Okay, I guess being motivating is less important than being clear. I'll edit.
@shawn1874: Okay, now it's D11, which hopefully is clear, but a plausible typo that someone might make.
3

In this case I'm prepared to suggest that the static_cast is not inherently safer. What the cast effectively does is tell the compiler to assume that the result will fit into the char and to not possibly warn you about data truncation. If you omit the cast, compilers are free to warn (especially if you request it) that data loss may be possible.

Not related to your question, but certainly related, static_cast is safer than both reinterpret_cast and C-style casts, because it has a much more limited set of legal conversions that it can perform.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.