overloading with templates

S

Sensei

Hi! I am trying to create a function template that converts a type to a
string, unsuccessfully! What did I think of? Overloading a function,
with a vector class, just to try if it works:

template<class T> string toString(T src) {...};

template<class T> string toString(vector<T> src) {...};


What happens is that the first one is called, ignoring the other when
passing a vector of, for example, double.

What am I missing?

Thanks for your help!
 
V

Victor Bazarov

Hi! I am trying to create a function template that converts a type to a
string, unsuccessfully! What did I think of? Overloading a function,
with a vector class, just to try if it works:

template<class T> string toString(T src) {...};

template<class T> string toString(vector<T> src) {...};


What happens is that the first one is called, ignoring the other when
passing a vector of, for example, double.

What am I missing?

The fact that the first one is still a better match for the argument
you're passing, maybe. Or the fact that the type of your argument makes
it impossible for the compiler to create an instance of the second
template (SFINAE)... We can't tell because you didn't post enough code.

V
 
F

Francesco S. Carta

Hi! I am trying to create a function template that converts a type to a
string, unsuccessfully! What did I think of? Overloading a function,
with a vector class, just to try if it works:

template<class T> string toString(T src) {...};

template<class T> string toString(vector<T> src) {...};


What happens is that the first one is called, ignoring the other when
passing a vector of, for example, double.

What am I missing?

As other posters have pointed out, you should show us the exact code
that is failing to do what you want. If I create a complete program
following your description I get something that does _not_ show the
problem you're lamenting:

#include <iostream>
#include <string>
#include <vector>

template<class T> std::string toString(T src) {
return "toString(T src)";
};

template<class T> std::string toString(std::vector<T> src) {
return "toString(vector<T> src)";
};

int main() {
// prints "toString(std::vector<T> src)" as expected
std::cout << toString(std::vector<double>()) << std::endl;
}
 
R

Ron AF Greve

Hi,

As the others said post code:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

template<class T> string toString(T src)
{
return "this is called";
};

template<class T> string toString(vector<T> src)
{
return "that is called";
}


int main()
{
cout << toString( 1.3 ) << endl;
cout << toString( vector<double>( 10 ) ) << endl;

return 0;
}


ronafgreve@informationsuperhighway:~/test$ g++ t.cc
ronafgreve@informationsuperhighway:~/test$ ./a.out
this is called
that is called


no problem here.



| Hi! I am trying to create a function template that converts a type to a
| string, unsuccessfully! What did I think of? Overloading a function,
| with a vector class, just to try if it works:
|
| template<class T> string toString(T src) {...};
|
| template<class T> string toString(vector<T> src) {...};
|
|
| What happens is that the first one is called, ignoring the other when
| passing a vector of, for example, double.
|
| What am I missing?
|
| Thanks for your help!

Regards, Ron AF Greve

http://informationsuperhighway.eu
 
S

Sensei

Ron AF Greve said:
Hi,

As the others said post code:


Ok, thanks to all of you, I'll post an exhaustive code then!

After fixing my code, now I am trying to do the other way round: from a
string to either a type or a vector. However, I cannot compile my code,
due to a "call to fromString(std::string &) is ambiguous":

#include <vector>
#include <string>
#include <iostream>

using namespace std;

template<class T> T fromString(string src)
{
return "output T" << endl;
};

template<class T> vector<T> fromString(string src)
{
return "output vector of T" << endl;
};


int main()
{
string s("3.1415");
cout << fromString<double>( s ) << endl;

return 0;
}


I hope my code is ok. I paid attention to specializing the call to the
template function, but as I said, I am learning C++ so I am still making
mistakes!

If I specialize the call with an explicit reference to a double output,
why is it still ambiguous?

Thanks & cheers!
 
Ö

Öö Tiib

Ok, thanks to all of you, I'll post an exhaustive code then!

After fixing my code, now I am trying to do the other way round: from a
string to either a type or a vector. However, I cannot compile my code,
due to a "call to fromString(std::string &) is ambiguous":

#include <vector>
#include <string>
#include <iostream>

using namespace std;

template<class T> T fromString(string src)
{
   return "output T" << endl;

};

template<class T> vector<T> fromString(string src)
{
   return "output vector of T" << endl;

};

int main()
{
   string s("3.1415");
   cout << fromString<double>( s ) << endl;

   return 0;

}

I hope my code is ok. I paid attention to specializing the call to the
template function, but as I said, I am learning C++ so I am still making
mistakes!

If I specialize the call with an explicit reference to a double output,
why is it still ambiguous?

Thanks & cheers!

You need to go few books back and discover C++ without templates
first. Your posted code is buggy because of something like that:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

double fromString( string src )
{
return 0.1;
};

vector<double> fromString( string src )
{
return vector<double>();
};

int main()
{
string s("3.1415");
cout << fromString( s ) << endl;

return 0;
}

What is ambiguous?
 
A

Alf P. Steinbach /Usenet

* Sensei, on 20.08.2010 10:02:
Ok, thanks to all of you, I'll post an exhaustive code then!

After fixing my code, now I am trying to do the other way round: from a
string to either a type or a vector. However, I cannot compile my code,
due to a "call to fromString(std::string&) is ambiguous":

#include<vector>
#include<string>
#include<iostream>

using namespace std;

template<class T> T fromString(string src)
{
return "output T"<< endl;
};

template<class T> vector<T> fromString(string src)
{
return "output vector of T"<< endl;
};


int main()
{
string s("3.1415");
cout<< fromString<double>( s )<< endl;

return 0;
}


I hope my code is ok. I paid attention to specializing the call to the
template function, but as I said, I am learning C++ so I am still making
mistakes!

Oh well. A C++ function has a /type/ consisting of its return type and the types
of the arguments, and wrt. type your two 'fromString' are different. However,
the return type does not matter with respect to overload resolution, that is,
with respect to choosing which function to call: the return type is not
considered, you can't overload on return type. The type minus the return type is
called the function /signature/, and that's all that matters.

Wrt. signature both your 'fromString' are identical.

One simple fix is to call the second function e.g. 'fromStringVector', invoked
like 'fromStringVector<double>( "blah" )'.

With that you'll get rid of the ambiguity, but you'll discover that your return
statements are not correct.

You can then use e.g. std::istringstream (inside the functions) to convert.


Cheers & hth.,

- Alf
 

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

Similar Threads

templates?? 2
templates 10
Templates 5
Templates with two params 4
Operator overloading with templates 2
Specialized Templates 3
Templates 4 a Nu-B 1
Operator overloading with templates 5

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top