By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,929 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,929 IT Pros & Developers. It's quick & easy.

Can a template function return a template type?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.