How can I do a template function that returns vector of templatetype?

R

Rob

I want to do something like this:

Code:
template <typename MyType>
std::vector<MYType> get(SomeClass c)
{
     ...elided...
}

int main()
{
    std::vector<std::vector<std::string> ret = get( c );
}

But the problem is it has no way (I can see) to differentiate based on
type. How do I do something like that?
 
D

Davis King

I want to do something like this:

Code:
template <typename MyType>
std::vector<MYType> get(SomeClass c)
{
...elided...

}

int main()
{
std::vector<std::vector<std::string> ret = get( c );}

But the problem is it has no way (I can see) to differentiate based on
type. How do I do something like that?

In your example you would say:
std::vector<std::vector<std::string> > ret =
get<std::vector<std::string> >( c );

That is, you would explicitly provide the template argument at the
point you call get() since in your case it isn't inferable from the
argument to the function. Although maybe this isn't what you are
asking?

-Davis
 
R

Rob

I want to do something like this:
Code:
template <typename MyType>
std::vector<MYType> get(SomeClass c)
{
     ...elided... [QUOTE]
}[/QUOTE]

int main()
{
    std::vector<std::vector<std::string> ret = get( c );} [QUOTE]

But the problem is it has no way (I can see) to differentiate based on
type. How do I do something like that?

In your example you would say:
  std::vector<std::vector<std::string> > ret =
get<std::vector<std::string> >( c );

That is, you would explicitly provide the template argument at the
point you call get() since in your case it isn't inferable from the
argument to the function.  Although maybe this isn't what you are
asking?

-Davis[/QUOTE]

I tried that and it still does not compile. I think it's because the
overloaded methods that will be created for the templated function
only differ by return type and not by args (the args are always the
same). Am I correct in that overloaded methods must differ in args?
 
I

Ian Collins

Rob said:
I tried that and it still does not compile. I think it's because the
overloaded methods that will be created for the templated function
only differ by return type and not by args (the args are always the
same). Am I correct in that overloaded methods must differ in args?

Yes.
 
B

brian tyler


If you need different behaviour based on the template type then the
thing to do is to require the function to pass a reference in to a
vector which can act as the return value:

template <class _Type1> void get( SomeType c, std::vector<_Type1>&
vect ) {
// Stuff

return vect;
}

template <class _Type2> void get( SomeType c, std::vector<_Type2>&
vect ) {
// Stuff

return vect;
}

Those are perfectly valid overloads.

An alternative would be to turn get into a function object and then
specialise it for each type
 
D

Davis King


Uh, I beg to differ. I have used similar templated functions
frequently with no trouble. Think about boost::lexical_cast for
example. That works just fine even if you try to cast a string to an
int and also a string to a double. But by the logic of this thread
that should be illegal because the overloaded methods created by the
template wouldn't differ in their arguments.

I also just compiled this program in gcc and it worked fine for me.

#include <vector>
#include <string>

using namespace std;

template <typename T>
vector<T> get(int c)
{
return vector<T>();
}

int main()
{
vector<vector<string> > ret1 = get<vector<string> >( 0 );
vector<string> ret2 = get<string >( 0 );
}

What exactly are you trying to compile that isn't working?

-Davis
 
R

Rob

Uh, I beg to differ.  I have used similar templated functions
frequently with no trouble.  Think about boost::lexical_cast for
example.  That works just fine even if you try to cast a string to an
int and also a string to a double.  But by the logic of this thread
that should be illegal because the overloaded methods created by the
template wouldn't differ in their arguments.

I also just compiled this program in gcc and it worked fine for me.

#include <vector>
#include <string>

using namespace std;

template <typename T>
vector<T> get(int c)
{
    return vector<T>();

}

int main()
{
    vector<vector<string> > ret1 = get<vector<string> >( 0 );
    vector<string> ret2 = get<string >( 0 );

}

What exactly are you trying to compile that isn't working?

-Davis

You're a genius!! The problem was in what I was specializing to. I'd
accidentally done:

vector<vector<string> > ret = get<vector<string> >( c );

when it should have been:

vector<vector<string> > ret = get<string>( c );

Thanks!
 
K

kepeng

You're a genius!! The problem was in what I was specializing to. I'd
accidentally done:

vector<vector<string> > ret = get<vector<string> >( c );

when it should have been:

vector<vector<string> > ret = get<string>( c );
Is this you want?

template <typename MyType>
std::vector<std::vector<MYType> > get(SomeClass c)
{
...elided...
}

int main()
{
std::vector<std::vector<std::string> ret = get<std::string>( c );
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top