G
Gianni Mariani
So I have a situation where I have all kinds of smart pointer types that
have an "*" deref operator and I want to create a simple template to
assign to any of them.
// ======== DerefTypeAssign ===========================================
/**
* This discovers the type of the dereferenced pointer and then assigns
* a dynamically downcasted version of the other.
*
* @return nothing
*/
template <typename w_Type, typename w_DerefType, typename w_Parent >
inline bool DerefTypeAssign(
w_Type & o_dest,
w_DerefType * i_ignored,
w_Parent * i_parent_ptr
) {
w_DerefType * l_newp = dynamic_cast< w_DerefType * >(i_parent_ptr);
if ( ! l_newp )
{
return false;
}
o_dest = l_newp;
return true;
}
Usage is from a template:
template <typename w_Type>
class ResolverReference
{
.....
w_Type & ref;
template <typename w_XX>
bool FixRef( w_XX & foo )
{
return DerefTypeAssign( ref, &*ref, &*foo );
}
};
here is it quite possible that ref has a null pointer.
OK OK - if according the the standard it's UB, does anyone know of a
practical situation where it would not work ?
I really really wish we had "typeof" ...
FYI, this is for some reference fix up code... In the input file all
the objects have names and they come in in any order. For simplicity I
want to have regular classes with regular members. As the objects are
read in, all the references (pointers, smart or not) are placed into a
resolver. The act of resolving is by calling the "FixRef" method above.
Anyone have a better idea ?
have an "*" deref operator and I want to create a simple template to
assign to any of them.
// ======== DerefTypeAssign ===========================================
/**
* This discovers the type of the dereferenced pointer and then assigns
* a dynamically downcasted version of the other.
*
* @return nothing
*/
template <typename w_Type, typename w_DerefType, typename w_Parent >
inline bool DerefTypeAssign(
w_Type & o_dest,
w_DerefType * i_ignored,
w_Parent * i_parent_ptr
) {
w_DerefType * l_newp = dynamic_cast< w_DerefType * >(i_parent_ptr);
if ( ! l_newp )
{
return false;
}
o_dest = l_newp;
return true;
}
Usage is from a template:
template <typename w_Type>
class ResolverReference
{
.....
w_Type & ref;
template <typename w_XX>
bool FixRef( w_XX & foo )
{
return DerefTypeAssign( ref, &*ref, &*foo );
}
};
here is it quite possible that ref has a null pointer.
OK OK - if according the the standard it's UB, does anyone know of a
practical situation where it would not work ?
I really really wish we had "typeof" ...
FYI, this is for some reference fix up code... In the input file all
the objects have names and they come in in any order. For simplicity I
want to have regular classes with regular members. As the objects are
read in, all the references (pointers, smart or not) are placed into a
resolver. The act of resolving is by calling the "FixRef" method above.
Anyone have a better idea ?