Stovia Smith
Stovia Smith

Reputation: 1

overloading template function

Currently, I encounter some difficulty in overloading a certain function. here's my code:

template<typename Value>
bool process(Value thisValue)
{
  return processAccordingToTheType(thisValue);
}

So, there are two overloaded function of processAccordingToTheType:

bool processAccordingToTheType(int thisValue){}
bool processAccordingToTheType(string thisValue){}

when I try to compile it, it said:

error C2665: 'processAccordingToTheType' : none of the 2 overloads could convert all the argument types

what do I need to do?

Update:

int main()
{
  int i = 1;
  process <int> (i);
}

Upvotes: 0

Views: 333

Answers (3)

ubi
ubi

Reputation: 4399

From your sample code I understand you need two things to be done:

  1. Call a type specific process function
  2. Restrict these calls to string and int types

Wrapping the processAccordingToType function inside process<T> is completely redundant: process<T> actually means 'process according to type'. The keyword here is 'template specialization'. You need to specialize your 'process according to type' method for int and string.

You can do this as below:

#include <iostream>

using namespace std;

template<typename T>
bool process(T t)
{
    // call a Compile-Time Assertion 
    cout << "I don't want this to be called." << endl;
}

template <>
bool process<int>(int i)
{
    cout << "process(int) called." << endl;
}


template <>
bool process<string>(string s)
{
    cout << "process(string) called." << endl;
}

int main()
{
    process(1);
    process(string("s"));
    process(1.0d);
}

Output:

process(int) called.
process(string) called.
I don't want this to be called.

Ideally, you want to prevent the users of your API calling process with other types. Allowing them to call and handling this at runtime (like it's done in my example) is not acceptable. You achieve this with Compile-Time Assertions. Read "Modern C++ Designs" by Andrei Alexandrescu for ways of doing that.

Upvotes: 3

msmith81886
msmith81886

Reputation: 2366

You can overload function templates with either a non-template function or another template function. Make sure that whatever you do, you test incrementally as template errors are notoriously hard to understand.

http://www.cplusplus.com/doc/tutorial/templates/

#include <iostream>

using namespace std;


template <typename Value>
bool processAccordingToTheType( Value thisValue ){
    cout << "Generic Type" << endl;
    return false;
}

bool processAccordingToTheType(int thisValue){
    cout << "int type" << endl;
    return true;
}

template <typename Value>
bool process( Value thisValue ){
    return processAccordingToTheType(thisValue);
} 

int main( int argc, char* argv[] ){

    cout << process( 1 ) << endl;
    cout << process( "Hello" ) << endl;

    return 0;
}

Upvotes: 0

krsteeve
krsteeve

Reputation: 1804

Look into template specialization. Does what you're looking for without deferring to another function based on type.

http://www.cprogramming.com/tutorial/template_specialization.html

Upvotes: 0

Related Questions