1

Is there any possibility to shorten this declaration as I use it very often in my code

For example, I use this to cast Object to my CustomObject using

dynamic_cast/static_cast<TCustomClassName*>(SenderObject)

Is there any possibility at all to shorten this using typedef static_cast<TCustomClassName*> or something like this making sure it's possible using cast operator in typedef?

Suhrob Samiev
  • 1,528
  • 1
  • 25
  • 57
  • Any solution with a macro is a bad idea. An inline templated function can shorten the length but I will recommend to keep it verbose. – Ram Sep 19 '12 at 07:10
  • 4
    If you find yourself doing this very often, you might need a redesign. – juanchopanza Sep 19 '12 at 07:20

4 Answers4

10

No, you cannot, and you SHOULD NOT! Do not define any macros for the cast operators, it will confuse the maintainers of your application code, and will cause havoc in your programming circles. These operators are there exactly with the reason to offer a readable way of telling the programmer, that a cast is happening here. Casts regardless that are used on an everyday basis cause confusion between programmers, so these keywords are there to help them. So, stuck to them and use them wisely. Do not revert back even to C style casting, the purpose of these operators is to offer a way of understanding what happens in the code. If you are unfamiliar with them read this: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
1

You can use templated functions instead of macros, that way you don't lose any type safety:

template<typename InputType, typename ReturnType>
void SCast(InputType Input, ReturnType &Ret)
{
    Ret = static_cast<ReturnType>(Input);
}

template<typename InputType, typename ReturnType>
void DCast(InputType Input, ReturnType &Ret)
{
    Ret = dynamic_cast<ReturnType>(Input);
}

template<typename InputType, typename ReturnType>
void RCast(InputType Input, ReturnType &Ret)
{
    Ret = reinterpret_cast<ReturnType>(Input);
}

Then you can use them like this:

TCustomClassName *cls;
SCast(SenderObject, cls); 

.

TCustomClassName *cls;
DCast(SenderObject, cls); 

.

TCustomClassName *cls;
RCast(SenderObject, cls); 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Use the keyboard shortcuts of your IDE. For example, in Eclipse, through code completion, it only takes a couple of keystrokes to enter static_cast<MyClass*>. If you need the same really often, you could even define your own keyboard macro to insert the boilerplate through one hotkey.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • I think code insight of ERS 2010 gets slower if your project gets bigger compared to MS VS 2010 intellisense. Using hotkey in that case is also a good idea but never used it before. – Suhrob Samiev Sep 20 '12 at 02:52
0

It is possible in this way :

auto ToUi16 = [](auto v)
{
    return static_cast<unsigned __int16>(v);
};

But it makes your code less readable.

rooltex
  • 134
  • 3
  • 18