473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template fns in non-template class

Hi all,

I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:

class A
{
public:
template<typena me T>
void f(key &k, T& t)
{
// need to store the address of t
}

void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}
};

and then usage could be:

A a;
string str = "hello";
double d = 3.2;
int i = 2;

a.f(1, str);
a.f (2, d);
a.f(3, i);
//....

a.g(); //do something to alter values of str, d, i

How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.

Thank you.
G.

Feb 1 '07 #1
2 1221
On Feb 1, 1:38 pm, garfunkelor...@ googlemail.com wrote:
Hi all,

I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:

class A
{
public:
template<typena me T>
void f(key &k, T& t)
{
// need to store the address of t
}

void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}

};

and then usage could be:

A a;
string str = "hello";
double d = 3.2;
int i = 2;

a.f(1, str);
a.f (2, d);
a.f(3, i);
//....

a.g(); //do something to alter values of str, d, i

How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.
You have to cast it to void*, something like this ought to work:

class A {
std::map<key, void*map_;
public:
template<class T>
void f(key k, T& t) {
map_[k] = static_cast<voi d*>(&t);
}
void* g(key k) {
return map_[k];
}
};

The problem with this is that the user calling g() have to know the
type of the object pointed to, but if it's known then you can cast it
back to a pointer of string, int, or whatever.

--
Erik Wikström

Feb 1 '07 #2
On 1 Feb, 13:01, "Erik Wikström" <eri...@student .chalmers.sewro te:
On Feb 1, 1:38 pm, garfunkelor...@ googlemail.com wrote:


Hi all,
I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:
class A
{
public:
template<typena me T>
void f(key &k, T& t)
{
// need to store the address of t
}
void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}
};
and then usage could be:
A a;
string str = "hello";
double d = 3.2;
int i = 2;
a.f(1, str);
a.f (2, d);
a.f(3, i);
//....
a.g(); //do something to alter values of str, d, i
How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.

You have to cast it to void*, something like this ought to work:

class A {
std::map<key, void*map_;
public:
template<class T>
void f(key k, T& t) {
map_[k] = static_cast<voi d*>(&t);
}
void* g(key k) {
return map_[k];
}

};

The problem with this is that the user calling g() have to know the
type of the object pointed to, but if it's known then you can cast it
back to a pointer of string, int, or whatever.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Thanks, I think that is the only way too.

Feb 1 '07 #3

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

Similar topics

12
4418
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
3742
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
3
12226
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
1
6891
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all non-word characters from the string: // PCRE syntax: $string = preg_replace("/(+)/", "-", $string);
4
5302
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note that these columns in the unique indexes are truly unique and don't constitute a collision hazard of any kind. Now, I personally wouldn't use unique where non-unique would do but I
2
6108
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
0
2335
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
12788
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
9
3796
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
29855
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
8421
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, 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...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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,...
0
8621
tracyyun
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...
0
7354
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, 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...
0
5643
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();...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.