M
Max
I am trying to find a way to eliminate vararg functions from my code
by packaging the input parameters in stringstreams. Here is an
oversimplified example of what I am trying to do:
Functions FUNC1 and FUNC2 delegate to functions func1 and func2,
respectively. The goal is to delegate to func1 and func2 using an
identical interface provided by the template function
template<typename F>FTEMPL that takes a stringstream as input and
determines which function to delegate by determining the type of the
generic parameter, F. Here is the code (previously I had a vararg
function instead of the template function):
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo.h>
using namespace std;
// internal functions
void func1(int, double);
void func2(float, char, string);
// wrapper functions
void FUNC1(int, double);
void FUNC2(float, char, string);
typedef void (*FUNC1_PTR)(int, double);
typedef void (*FUNC2_PTR)(float, char, string);
template<typename F>
void FTEMPL(stringstream ss)
{
if(typeid(F).name() == typeid(FUNC1_PTR).name())
{
int n;
double d;
ss >> n >> d;
func1(n, d);
}
if(typeid(F).name() == typeid(FUNC2_PTR).name())
{
float f;
char c;
string s;
ss >> f >> c >> s;
func2(f, c, s);
}
else
{
cout << "Unrecognized type!" << endl;
}
}
void FUNC1(int n, double d)
{
cout << "FUNC1" << endl;
stringstream ss;
ss << n << endl;
ss << d << endl;
FTEMPL<FUNC1_PTR>(ss);
}
void FUNC2(float f, char c, string s)
{
cout << "FUNC2" << endl;
stringstream ss;
ss << f << endl;
ss << c << endl;
ss << s << endl;
FTEMPL<FUNC2_PTR>(ss);
}
void func1(int n, double d)
{
cout << "func1" << endl;
}
void func2(float f, char c, string s)
{
cout << "func2" << endl;
}
int main(int argc, char* argv[])
{
FUNC1(0, 0.0);
FUNC2(0.0f, '0', "bingo");
return 0;
}
When I run the program in debug mode (I use VC++ 6.0), I get the
following error:
HEAP[testVarArg.exe]: Invalid Address specified to RtlValidateHeap(
2f0000, 2f48e0 )
when the template function returns. When I step into the template
function from FUNC1, I see that typeid(F).name() is FUNC2_PTR instead
of FUNC1_PTR.
What am I doing wrong???
Thanks!
P.S. This example is obviously an overkill, but in the actual program
there is quite some code that would go into the template function
before the if conditions and there are 25 functions with different
signatures, not just 2.
by packaging the input parameters in stringstreams. Here is an
oversimplified example of what I am trying to do:
Functions FUNC1 and FUNC2 delegate to functions func1 and func2,
respectively. The goal is to delegate to func1 and func2 using an
identical interface provided by the template function
template<typename F>FTEMPL that takes a stringstream as input and
determines which function to delegate by determining the type of the
generic parameter, F. Here is the code (previously I had a vararg
function instead of the template function):
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo.h>
using namespace std;
// internal functions
void func1(int, double);
void func2(float, char, string);
// wrapper functions
void FUNC1(int, double);
void FUNC2(float, char, string);
typedef void (*FUNC1_PTR)(int, double);
typedef void (*FUNC2_PTR)(float, char, string);
template<typename F>
void FTEMPL(stringstream ss)
{
if(typeid(F).name() == typeid(FUNC1_PTR).name())
{
int n;
double d;
ss >> n >> d;
func1(n, d);
}
if(typeid(F).name() == typeid(FUNC2_PTR).name())
{
float f;
char c;
string s;
ss >> f >> c >> s;
func2(f, c, s);
}
else
{
cout << "Unrecognized type!" << endl;
}
}
void FUNC1(int n, double d)
{
cout << "FUNC1" << endl;
stringstream ss;
ss << n << endl;
ss << d << endl;
FTEMPL<FUNC1_PTR>(ss);
}
void FUNC2(float f, char c, string s)
{
cout << "FUNC2" << endl;
stringstream ss;
ss << f << endl;
ss << c << endl;
ss << s << endl;
FTEMPL<FUNC2_PTR>(ss);
}
void func1(int n, double d)
{
cout << "func1" << endl;
}
void func2(float f, char c, string s)
{
cout << "func2" << endl;
}
int main(int argc, char* argv[])
{
FUNC1(0, 0.0);
FUNC2(0.0f, '0', "bingo");
return 0;
}
When I run the program in debug mode (I use VC++ 6.0), I get the
following error:
HEAP[testVarArg.exe]: Invalid Address specified to RtlValidateHeap(
2f0000, 2f48e0 )
when the template function returns. When I step into the template
function from FUNC1, I see that typeid(F).name() is FUNC2_PTR instead
of FUNC1_PTR.
What am I doing wrong???
Thanks!
P.S. This example is obviously an overkill, but in the actual program
there is quite some code that would go into the template function
before the if conditions and there are 25 functions with different
signatures, not just 2.