Ondra,
Your RemoveRef typedef solved the problem. Congratulations! :)
1. I need to deference because I have the following code:
class BaseType {
protected:
const char *mName;
public:
BaseType() : mName(NULL) {}
BaseType( const BaseType& vrBaseType) : mName( NULL) {}
virtual void * GetAddress() const = 0;
virtual size_t GetSize() const = 0;
const char *GetName() const { return mName; }
};
template <typename T>
class VariantType : BaseType {
private:
const T& mItem;
public:
VariantType( const T& vItem) : mItem(vItem), BaseType() { mName =
typeid(vItem).name(); }
void* GetAddress() const { return (void *)&mItem; }
size_t GetSize() const { return sizeof(mItem); }
};
All I have access is BaseType, and it's type (because of template
completion).
template <class T_INSTANCE, typename T_FUNCTION >
class EventHandler : public IEventHandler {
protected:
T_INSTANCE* mpInstance;
T_FUNCTION mpFunction;
public:
EventHandler( T_INSTANCE* vpInstance, T_FUNCTION vpFunction):
mpInstance( vpInstance), mpFunction( vpFunction) {
}
void Execute( ParamHelper& vrHelper) {
vrHelper.Call( mpInstance, mpFunction);
}
};
the PAramHelper Call:
template <class T_INSTANCE, class A1>
void Call( T_INSTANCE* vpInstance,
void (T_INSTANCE::*vpFunction)(A1)) {
AssertType<A1>( 0);
(vpInstance->*vpFunction)(*(RemoveRef<A1>::Type
*)mArguments[0]->GetAddress()); }
And ParamHelper was previously parsed:
template<class A1>
void Parse( A1& arg1) {
Reset();
mArguments.Append() = (BaseType *) new VariantType<A1>( arg1);
}
Thank you all!
ondra.holub wrote:
1. I do not understand, why are you using void* instead of
template <typename T>
void do_something( T* ptr, void(*funct)(T arg1)) {
funct( *ptr);
}
2. If you are writing template parametrized with type T and you want to
have type U which is defined as
(a) if T is reference to X, U is X
(b) in any other case U is T
You can write it this way:
// Reference removal code
template<typename T>
struct RemoveRef
{
typedef T Type;
};
template<typename T>
struct RemoveRef<T&>
{
typedef T Type;
};
// Now you can use it
template<typename T>
struct HereINeedParameterWithoutReference
{
typedef
typename RemoveRef<T>::Type TWithoutReference;
};
3. An now forget previous rows. You need to change the template this
way and you have all you need:
template <typename T, typename F>
void do_something(T* ptr, F& funct)
{
funct(*ptr);
}