473,402 Members | 2,055 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,402 software developers and data experts.

Can a template function return a template type?

Hi,

Created the template function below but the g++ compiler keeps
complaining "no matching function for call to....".

Is there a way to make it work? Thank you.

==========snip part of class method===========
template <class TYPE> TYPE *field( const std::string &field_name ) {
if( !b_default ) {
throw runtime_error("{persistent}<field>: Not in default "
"mode, cannot use the field method!");
}

std::map<std::string, std::vector<std::string> >::iterator iter;
iter = m_table.find( field_name );
if( m_table.end()==iter ) {
std::cerr << "{persistent}<associate>: The field, '" <<
field_name << "' is not in the table <" << tname <<
">." << std::endl;
throw std::runtime_error("{persistent}<associate>: The field,
'" +
field_name + "' is not in the table
<" + tname +
">");
}

std::string d_type = (m_table[ field_name ])[0];

if( "string"==d_type || "date"==d_type ) {
dbUtils::data_type<std::string>* current =
(dbUtils::data_type<std::string>*) data_values[ field_name
];
return current->value;
}
else if( "double"==d_type ) {
dbUtils::data_type<double>* current =
(dbUtils::data_type<double>*) data_values[ field_name ];
return current->value;
}
else if( "int"==d_type ) {
dbUtils::data_type<int>* current =
(dbUtils::data_type<int>*) data_values[ field_name ];
return current->value;
}
else if( "long"==d_type ) {
dbUtils::data_type<long>* current =
(dbUtils::data_type<long>*) data_values[ field_name ];
return current->value;
}
else if( "bool"==d_type ) {
dbUtils::data_type<bool>* current =
(dbUtils::data_type<bool>*) data_values[ field_name ];
return current->value;
}
else {
std::cerr << "{persistent}<field>: Encounter unknown "
"data type" << std::endl;
throw std::runtime_error("{persistent}<field>: Encounter "
"unknown data type");
}
}
Jul 22 '05 #1
2 1352
On Sat, 13 Dec 2003 09:33:44 -0800, Damon wrote:
Hi,

Created the template function below but the g++ compiler keeps
complaining "no matching function for call to....".

Is there a way to make it work? Thank you.

==========snip part of class method===========
template <class TYPE> TYPE *field( const std::string &field_name ) {


How would the compiler know what type TYPE is? The answer is that you have
to explicitly tell it when you call the function, eg:

int* pt = field<int>("string");

Josh
Jul 22 '05 #2
Thanks alot Josh, it works!
How would the compiler know what type TYPE is? The answer is that you have
to explicitly tell it when you call the function, eg:

int* pt = field<int>("string");

Josh

Jul 22 '05 #3

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

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
3
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int...
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: 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
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,...
0
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...
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.