0

Suppose i don't like the name of static_cast operator and want to wrap it in a function with a different name, say fancy_static_cast but perfectly preserving the semantics. How should i do it? More specifically does static_cast accept it's argument by value or by reference? Or does it depend on the argument expression? Should i provide several overloads or will something like this do the trick?

template <typename To, typename From>
To fancy_static_cast(From&& from)
{
    return static_cast<To>(std::forward<From>(from));
}
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
  • [`static_cast`](http://en.cppreference.com/w/cpp/language/static_cast) is not a function, it's a special compiler keyword that is handled completely by the compiler at compilation time. – Some programmer dude Jun 17 '13 at 10:39
  • @JoachimPileborg I never said it was a function, nor it is a compiler directive. `static_cast` is an operator. But how is this important anyway? – yuri kilochek Jun 17 '13 at 10:46
  • 2
    @yurikilochek Operators don't have function semantics - they don't take arguments "by value" or "by reference." They are defined in terms of l-values and r-values, and can very well contain logic like "if it's a particular l-value, then, ..., otherwise treat it as r-value..." – Angew is no longer proud of SO Jun 17 '13 at 11:09

1 Answers1

0

Suppose i don't like the name of static_cast operator and want to wrap it in a function with a different name, say fancy_static_cast but perfectly preserving the semantics. How should i do it?

You shouldn't.

Instead, you say static_cast when you mean static_cast. Otherwise you will achieve only a lack of maintainability: Readers of your code will at first not recognize the static_cast, and when they look at the function they won't have an idea why the heck you did this and waste brainpower just to realize that you do essentially nothing.

Be clear and crisp with your code. Say what you mean, don't delude the readers (including yourself) by being overly fancy. Don't use misleading namings, instead try to use the parts of the language everybody knows by heart. Give yourself and others as few as possible opportunities to misunderstand what you have written.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90