Can a template function return a template type?

Discussion in 'C++' started by Damon, Dec 13, 2003.

  1. Damon

    Damon Guest

    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");
    }
    }
    Damon, Dec 13, 2003
    #1
    1. Advertising

  2. 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
    Josh Sebastian, Dec 13, 2003
    #2
    1. Advertising

  3. Damon

    Damon Guest

    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
    Damon, Dec 15, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    450
    Victor Bazarov
    Nov 19, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    786
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    2
    Views:
    416
    Aleksey Loginov
    Nov 7, 2005
  4. Diego Martins
    Replies:
    5
    Views:
    474
    Diego Martins
    Sep 5, 2006
  5. Luca Forlizzi
    Replies:
    4
    Views:
    441
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page