# [C++] Dynamic types using virtual functions

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I thought about making dynamic (runtime) expression templates. I thought about using polymorphism as it allows to handle data of any type (e.g. boost::any). An appriopriate virtual function would return a new object of a new type. However, I'm 99% sure this is impossible (ignore the memory leak for the safe of example):
class base {
public:
virtual base *make() = 0;
};

template <typename T>
class X : public base {
public:

X(const T& t) : value(t) { }

base *make()
{
return new X<X<T> >(value); // creates a new type
}

private:
T value;
};

int main()
{
X<int> x(5);

x.make();
}

This causes:
Error	1	fatal error C1001: An internal error has occurred in the compiler.	d:\temp\vstest1\vstest1.cpp	15

under VS 2008 and
[C++ Error] Unit1.cpp(21): E2178 VIRDEF name conflict for 'X< lots of X<int> lots of > *'
[C++ Error] Unit1.cpp(38): E2178 VIRDEF name conflict for 'X< lots of X<int> > > lots of > >'
[C++ Error] Unit1.cpp(38): E2178 VIRDEF name conflict for 'X<X<lot sof X<int> > lots of >::make()'

under Borland Dev Studio 2008 So is it 100% impossible? EDIT: Comeau Online spits enermous amounts of < and > :-) [Edited by - rozz666 on August 3, 2008 4:34:17 PM]

##### Share on other sites
The point of an expression template is that it converts a runtime expression evaluation into a compile-time representation using templates (which can then be optimized by the compiler). So, a runtime expression template would convert a runtime expression evaluation into a runtime representation (which can be done by keeping the original expression).

In short, what do you call 'dynamic expression template', exactly?

##### Share on other sites
I have made an expression-template-based library for manipulating 2d arrays (eg. +, -, sin() etc.). The problem is I want to make a parser (parse A + B * C) that will output an expression I could then use to compute the results.

##### Share on other sites
Quote:
 Original post by rozz666I have made an expression-template-based library for manipulating 2d arrays (eg. +, -, sin() etc.). The problem is I want to make a parser (parse A + B * C) that will output an expression I could then use to compute the results.

That's not a job for templates—runtime polymorphism, as required by expressions, is different from compile-time polymorphism that templates provide, and should instead be achieved through inheritance.

Think about it this way: if you want to use your expression templates, then the type of the resulting expression template must be known at compile-time (because it's a type) and therefore, since the type of the expression template maps to the expression itself, the expression must also be known at compile-time. Since this is impossible in a parser situation, you have no choice but to use runtime polymorphism instead.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by rozz666I have made an expression-template-based library for manipulating 2d arrays (eg. +, -, sin() etc.). The problem is I want to make a parser (parse A + B * C) that will output an expression I could then use to compute the results.

That's not a job for templates—runtime polymorphism, as required by expressions, is different from compile-time polymorphism that templates provide, and should instead be achieved through inheritance.

Think about it this way: if you want to use your expression templates, then the type of the resulting expression template must be known at compile-time (because it's a type) and therefore, since the type of the expression template maps to the expression itself, the expression must also be known at compile-time. Since this is impossible in a parser situation, you have no choice but to use runtime polymorphism instead.

It seems I will have to. I tried to do a workaround but it my example the compiler has to generate all possible make() functions and there are infinitely many, am I right?

##### Share on other sites
Quote:
 Original post by rozz666It seems I will have to. I tried to do a workaround but it my example the compiler has to generate all possible make() functions and there are infinitely many, am I right?

Yes. There's one type per expression, and an infinite number of expressions.