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<typena me 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)(in t, double);
typedef void (*FUNC2_PTR)(fl oat, char, string);
template<typena me F>
void FTEMPL(stringst ream ss)
{
if(typeid(F).na me() == typeid(FUNC1_PT R).name())
{
int n;
double d;
ss >> n >> d;
func1(n, d);
}
if(typeid(F).na me() == typeid(FUNC2_PT R).name())
{
float f;
char c;
string s;
ss >> f >> c >> s;
func2(f, c, s);
}
else
{
cout << "Unrecogniz ed type!" << endl;
}
}
void FUNC1(int n, double d)
{
cout << "FUNC1" << endl;
stringstream ss;
ss << n << endl;
ss << d << endl;
FTEMPL<FUNC1_PT R>(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_PT R>(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. 3 5511
My result in MS c++.net 2003:
FUNC1
func1
Unrecognized type!
FUNC2
func2
I changed the code from "void FTEMPL(stringst ream ss)"
to "void FTEMPL(stringst ream& ss)".
Vince
"Max" <mi****@yahoo.c om> wrote in message
news:7b******** *************** ***@posting.goo gle.com... 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<typena me 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)(in t, double); typedef void (*FUNC2_PTR)(fl oat, char, string);
template<typena me F> void FTEMPL(stringst ream ss) { if(typeid(F).na me() == typeid(FUNC1_PT R).name()) { int n; double d;
ss >> n >> d; func1(n, d); } if(typeid(F).na me() == typeid(FUNC2_PT R).name()) { float f; char c; string s;
ss >> f >> c >> s; func2(f, c, s); } else { cout << "Unrecogniz ed type!" << endl; } }
void FUNC1(int n, double d) { cout << "FUNC1" << endl; stringstream ss; ss << n << endl; ss << d << endl;
FTEMPL<FUNC1_PT R>(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_PT R>(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.
On 30 Jun 2004 00:43:24 -0700, Max <mi****@yahoo.c om> wrote:
[] template<typena me F> void FTEMPL(stringst ream ss)
[]
Standard stream are not copyable. You might want to pass it by reference.
{ if(typeid(F).na me() == typeid(FUNC1_PT R).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_cas t<void(*)()>(t) ;
}
void f(std::stringst ream& ss, void(*id)())
{
if(void_fun_ptr _cast(&FUNC1) == id)
{
}
if(void_fun_ptr _cast(&FUNC2) == id)
{
}
else
{
}
}
int main()
{
std::stringstre am s;
f(s, void_fun_ptr_ca st(&FUNC1));
f(s, void_fun_ptr_ca st(&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
> 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<typena me 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
-- -- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 which is compiled and works fine.
############################################
|
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 `eck<>' for `void blah<int>::eck(int)'
does not match any template declaration
ttest.cpp:23: syntax error before `{' token
ttest.cpp:25: syntax error before `<<' token
Note that 'gak' (a template function in a template class) works, as
|
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)
|
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>
|
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 subsequent Matrix class. In
MatrixInterface class, I overloaded the << operator by calling a pure
virtual function PrintDebugMessage(ostream &os); then I can implement
the function on individual Matrix classes later.
| |
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 to call
a method defined in A's base class which at runtime determines the
template parameters (I know ahead what is allowed) and calls a
templated member function B with A's template parameters.
|
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 class itself, with all *its*
parameters filled out (in the form of a typedef.) I can't work out how
to break apart the typedef to reveal what data types were used to create
it in the first place.
Here is some example code that demonstrates the...
|
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 Gurtovoy. I’m
not doing this because I want to be a Meta Programming guru (because a
lot of that stuff looks too crazy for use in the real world).
Rather I want to learn heavyweight templates and this is the only
|
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() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";
The reason I need this is that in our design every class of a certain
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |