K
Kraus Christof
Hi,
this problem is similar to the one inquisitive posted earlier today:
to speed up our code I just tried to make use of the 'Curiously
Recursive Template Pattern'... and failed.
The code I'm working on tracks particles (up to a few millions) for a
some tousand time steps. For each time step and each particle a methode
'getExternalField' is called. This method is implemented in different
classes derived from a class 'Component'.
For anyone familiar with particle accelerators, the method returns the
electromagnetic field of elements such as solenoids, rf-cavities aso.
(just differen electromagnetic devices to accelerate and focus
particles). It is (up to now) declared virtual in the base class. Since
it is called so often, it would be nice if we could avoid virtual
functions while still have the flexibily of them.
With the CRT Pattern I would define a solenoid as
class Solenoid: Component<Solenoid>
{
...
};
Now the problem is that I want to have pointers to objects of these
derived classes in an STL list (not only a list of solenoids, but mixed
with other types). For example
Solenoid mySolenoid; //is derived from Component<Solenoid>
RFCavity myRFCavity; //is derived from Component<RFCavity>
list<Component<???>* > myList;
myList.push_back(&mySolenoid);
myList.push_back(&myRFCavity);
list<Component<???>*>::iterator it;
for (int i = 0; i < LargeNumberSteps; i++){
for (int j = 0; j < LargeNumberParticles; j++){
for( it = myList.begin(); it != myList.end(); it++){
(*it).getExternalField(R[j],E,B);
}
}
}
Is this possible to implement without using virtual functions? If yes,
how would this solution look like? Thanks for your help!
this problem is similar to the one inquisitive posted earlier today:
to speed up our code I just tried to make use of the 'Curiously
Recursive Template Pattern'... and failed.
The code I'm working on tracks particles (up to a few millions) for a
some tousand time steps. For each time step and each particle a methode
'getExternalField' is called. This method is implemented in different
classes derived from a class 'Component'.
For anyone familiar with particle accelerators, the method returns the
electromagnetic field of elements such as solenoids, rf-cavities aso.
(just differen electromagnetic devices to accelerate and focus
particles). It is (up to now) declared virtual in the base class. Since
it is called so often, it would be nice if we could avoid virtual
functions while still have the flexibily of them.
With the CRT Pattern I would define a solenoid as
class Solenoid: Component<Solenoid>
{
...
};
Now the problem is that I want to have pointers to objects of these
derived classes in an STL list (not only a list of solenoids, but mixed
with other types). For example
Solenoid mySolenoid; //is derived from Component<Solenoid>
RFCavity myRFCavity; //is derived from Component<RFCavity>
list<Component<???>* > myList;
myList.push_back(&mySolenoid);
myList.push_back(&myRFCavity);
list<Component<???>*>::iterator it;
for (int i = 0; i < LargeNumberSteps; i++){
for (int j = 0; j < LargeNumberParticles; j++){
for( it = myList.begin(); it != myList.end(); it++){
(*it).getExternalField(R[j],E,B);
}
}
}
Is this possible to implement without using virtual functions? If yes,
how would this solution look like? Thanks for your help!