473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

template and typeid

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.
Jul 22 '05 #1
3 5498
My result in MS c++.net 2003:

FUNC1
func1
Unrecognized type!
FUNC2
func2

I changed the code from "void FTEMPL(stringstream ss)"
to "void FTEMPL(stringstream& ss)".

Vince
"Max" <mi****@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
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.

Jul 22 '05 #2
On 30 Jun 2004 00:43:24 -0700, Max <mi****@yahoo.com> wrote:

[]
template<typename F>
void FTEMPL(stringstream ss)
[]

Standard stream are not copyable. You might want to pass it by reference.
{
if(typeid(F).name() == typeid(FUNC1_PTR).name())
[]

Why do you compare type_info's names instead of type_info's themselves?

I'm not sure wheather you need a template parameter here because you use it only as an id which may be passed as well as a usual function argument:

void FUNC1(int, double);
void FUNC2(float, char, std::string);

template<class T>
inline
void(*void_fun_ptr_cast(T* t))()
{
return reinterpret_cast<void(*)()>(t);
}

void f(std::stringstream& ss, void(*id)())
{
if(void_fun_ptr_cast(&FUNC1) == id)
{
}
if(void_fun_ptr_cast(&FUNC2) == id)
{
}
else
{
}
}

int main()
{
std::stringstream s;
f(s, void_fun_ptr_cast(&FUNC1));
f(s, void_fun_ptr_cast(&FUNC2));
}

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 )


Have you enabled RTTI in the compiler options?

--
Maxim Yegorushkin
Jul 22 '05 #3
> 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>


<typeinfo.h>??? No such Standard Header exists.

-- --
Abstraction is selective ignorance.
-Andrew Koenig
-- --
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
2
by: mosfet | last post by:
Hi I would like to write a template method, something like : template <typename Type> int MyClass::_Foo(vector<Type> MyVec) { if (int type) ..... if (long type)
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
4
by: Jason | last post by:
/* I have never use template before, so bear with me. Here is what I am trying to do: I declare an abstract base class MatrixInterface with template to define an interface for all my...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
3
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.