[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Compiler Issue or Bad Template

sccr13plyr

12/12/2008 7:50:00 PM

Hello,

I have two related algorithms at our site and are adding a new third
algorithm. There is a lot of redundant code and want to refactor out
as much as possible and have the compiler do all of the work. The
concept is to combine all three algorithms into one method.

This is fine, but we then have some unneeded comparisons and code
making their way into the executable. So, I templatized the class
looking for the compiler to generate the specializations for the
declared classes.

Alg 1 becomes class 1::GenericLogic()
Alg 2 becomes class 2::GenericLogic()
Alg 3 becomes class 3::GenericLogic()

The desire is to have the compiler use template parameters to optimize
out unused branches for a particular class instantiation. Thus,
class1::GenericLogic() machine code looks almost exactly like Alg 1
machine code, class 2::GenericLogic() looks almost exactly like Alg 2,
and so on...

I placed a “stupid” output in a branch that should be optimized away.
However, it shows up in the executables on both IA VMS and Windows.

On VMS, the compiler knows that there is branching on an unchanging
value. The “good” news is that the compiler seems to be generating
three separate methods. So, it should be a simple matter of
optimization for each instance. So, is this a declaration issue that
I need to correct with the template or a compiler issue?

TIA,

sccr13plyr

File: AlgorithmTemplate.h
#include <stdio.h>

#ifdef __VMS
#pragma message disable BOOLEXPRCONST
#endif

class BaseAlgorithm
{
public: virtual ~BaseAlgorithm (){}

public: inline void Display (const int id, const char* output)
{
(void) printf ("Algorithm: %d - %s\n", id, output);
}
};

template<int algorithmId_ = 0,
bool checkPriceChanged_ = false,
bool specificCode_ = false,
bool currentTransOnly_ = false>
class GenericAlgorithm : public BaseAlgorithm
{

public: GenericAlgorithm (){}
public: virtual ~GenericAlgorithm (){}
public: inline void GenericLogic (int* data, int* env, char*
retInfo)
{
if (data == NULL)
if (env == NULL)
if (retInfo == NULL)
{
//
}

if (checkPriceChanged_)
{
if (specificCode_)
{
if (currentTransOnly_)
{
Display (algorithmId_, "Check Price,
Specific Code, Current Tran");
}
else
{
if (data != env)
{
Display(0, "stupid\n");
}

Display (algorithmId_, "Check Price,
Specific Code, All Trans");
}
}
else
{
if (currentTransOnly_)
{
Display (algorithmId_, "Check Price, All
Code, Current Tran");
}
else
{
Display (algorithmId_, "Check Price, All
Code, All Tran");
}
}
}
else
{
if (specificCode_)
{
if (currentTransOnly_)
{
Display (algorithmId_, "Ignore Price,
Specific Code, Current Tran");
}
else
{
Display (algorithmId_, "Ignore Price,
Specific Code, All Trans");
}
}
else
{
if (currentTransOnly_)
{
Display (algorithmId_, "Ignore Price, All
Code, Current Tran");
}
else
{
Display (algorithmId_, "Ignore Price, All
Code, All Trans");
}
}
}
}
};

typedef GenericAlgorithm<246, true, false, false> theAlg;

File: main.cpp
#include <stdlib.h>
#include "AlgorithmTemplate.h"

int main (void)
{
GenericAlgorithm<159, true, true, true> test;
test.GenericLogic (NULL, NULL, NULL);

GenericAlgorithm<225, true, false, true> newTest;
newTest.GenericLogic (NULL, NULL, NULL);

theAlg *oldTest = new theAlg;
oldTest->GenericLogic (NULL, NULL, NULL);

return EXIT_SUCCESS;
}