LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2005, 04:31 PM   #1
astorm
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware
Posts: 23

Rep: Reputation: 15
Can't use template with std::map


I'm writing a small scripting language. I'm using a global std::map to store variables, which can be of any type. However, I can't do
Code:
template <class T>
std::map< string, T > varTable;
outside a function, and I can't do
Code:
void * varTable;

template <class T>
void makeTable()
{
    map< string, T > tVarTable;
    varTable = &tVarTable;
}
because it complains it can't find the function, and I'd probably have problems with dereferencing varTable. If there's no elegant way to do this, I'll have to create a Scalar struct with a void* value and string type. Is there any better way?

Thanks in advance,
Alek
 
Old 11-03-2005, 04:51 PM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
You are a dangerous man
I got it to create an inherited class like this:
Code:
#include <map>

template <class T>
class varTable : public std::map< std::string, T >
{
    // Your extensions here?  Maybe you do not want public inheritance?
}; 

int main( int argc, char **argv )
{
    varTable< double > doublesTable;

    return 0;
}
Not sure if this is what you wanted.
 
Old 11-03-2005, 05:14 PM   #3
astorm
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks. That would work, but it isn't quite what I wanted. I'd have to create a varTable for every possible type of variable. But hey, that still isn't as clunky as what I had before. I'll go ahead and write the code that way, and change it later if I find a better one. Gives me that adrenaline rush. Thanks again.
 
Old 11-03-2005, 06:55 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The problem is that the compiler can't use Koenig Lookup to figure out the type you need since you have no argument; if the function took an argument of type 'T' (or something that uses it, like 'vector <T>'), then the compiler could find out what type you need. Since you have no argument, you need to be explicit with the type in the call:
Code:
makeTable <double> ();
.

FYI: if you DO have an argument, but it can't be used to deduce ALL template parameters, such as
Code:
template <class A, class B> void Function(const A&);
place the deducible template arguments first, and those not deducible last. Then call the function like this:
Code:
char arg;
Function <int> (arg);
The compiler will know 'A' is 'char' and you tell it that 'B' is 'int'.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map question markhod Programming 1 08-22-2005 08:18 AM
Daemon Template thekonqueror Programming 0 08-15-2005 02:07 PM
SYSERR(root): hash map "generics": missing map file /etc/mail/genericstable.db? singying304 Linux - Networking 4 02-28-2005 06:49 AM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
c++ template help ashirazi Programming 4 11-16-2004 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration