'Templated operation on type-erased type

I have a template class val -

template <typename T>
class val;

This has the * operator overloaded for any pair of types T, T2. Basically a member -

template <typename T2>
void operator * (const val<T2>&);

Now I have another class val_holder that uses type erasure to hold val -

class val_holder;

template <typename T>
class val_holder_impl: public val_holder {
    val<T> m_val;
};

Finally in a separate class I have a template function that foo<T> that defines a val<T> x and receives as argument a val_holder* y.

I need to be able to invoke the * operator on x and the m_val inside the y.

I cannot use dynamic_cast because the template argument for y's precise type isn't known.

The other solution I tried is to have a virtual function in val_holder implemented by val_holder_impl<T> that invokes the * operator. But passing x would require the argument of the virtual function to be templated.

Any suggestions on how to get around this pattern?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source