Once upon a time, I created the following template to get a statement when I execute static_cast, but the type is not what I assume:
template <class Type, class SourceType>
Type static_cast_checked(SourceType item)
{
Assert(!item || dynamic_cast<Type>(item));
return static_cast<Type>(item);
}
Today I wanted to create an option that will work not only with pointers, but also with links:
template <class Type, class SourceType>
Type &static_cast_checked(SourceType &item)
{
Assert(dynamic_cast<Type *>(&item));
return static_cast<Type>(item);
}
However, the compiler does not seem to use this overload when I use a link to another link. I am afraid that I do not understand the rules for resolving patterns in order to understand why, or to be able to create an option that works.
Note. I cannot catch bad_cast exceptioninstead of checking dynamic_cast<Type *>for NULL, because exceptions for this project are disabled.
source
share