Can a template function return a template type?

D

Damon

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");
}
}
 
J

Josh Sebastian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top