473,516 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Rob
I want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. template <typename MyType>
  2. std::vector<MYTypeget(SomeClass c)
  3. {
  4. ...elided...
  5. }
  6.  
  7. int main()
  8. {
  9. std::vector<std::vector<std::stringret = get( c );
  10. }
  11.  
But the problem is it has no way (I can see) to differentiate based on
type. How do I do something like that?
Jun 27 '08 #1
9 2579
On Apr 11, 9:38 pm, Rob <someidunknown1...@yahoo.comwrote:
I want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. template <typename MyType>
  2. std::vector<MYTypeget(SomeClass c)
  3. {
  4.      ...elided...
  5. }
  6. int main()
  7. {
  8.     std::vector<std::vector<std::stringret = get( c );}
  9.  

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
Jun 27 '08 #2
Rob
On Apr 11, 9:55*pm, Davis King <davis...@gmail.comwrote:
On Apr 11, 9:38 pm, Rob <someidunknown1...@yahoo.comwrote:
I want to do something like this:
Expand|Select|Wrap|Line Numbers
  1.  template <typename MyType>
  2.  std::vector<MYTypeget(SomeClass c)
  3.  {
  4.  * * *...elided...
Expand|Select|Wrap|Line Numbers
  1.         
  2.                  }
  •  
  •         
  •                  int main()
  •  {
  •  * * std::vector<std::vector<std::stringret = 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 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?
    Jun 27 '08 #3
    Rob wrote:
    >
    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.

    --
    Ian Collins.
    Jun 27 '08 #4
    Rob
    On Apr 11, 10:46*pm, Ian Collins <ian-n...@hotmail.comwrote:
    Rob wrote:
    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.

    --
    Ian Collins.
    Thanks!
    Jun 27 '08 #5
    On Apr 12, 3:56 am, Rob <someidunknown1...@yahoo.comwrote:
    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.comwrote:
    Rob wrote:
    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.
    --
    Ian Collins.

    Thanks!
    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 _Type1void get( SomeType c, std::vector<_Type1>&
    vect ) {
    // Stuff

    return vect;
    }

    template <class _Type2void 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
    Jun 27 '08 #6
    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.comwrote:
    Rob wrote:
    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.

    --
    Ian Collins.
    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<Tget(int c)
    {
    return vector<T>();
    }

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

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

    -Davis

    Jun 27 '08 #7
    Rob
    On Apr 12, 9:24*am, Davis King <davis...@gmail.comwrote:
    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.comwrote:
    Rob wrote:
    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.
    --
    Ian Collins.

    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<Tget(int c)
    {
    * * return vector<T>();

    }

    int main()
    {
    * * vector<vector<string ret1 = get<vector<string( 0 );
    * * vector<stringret2 = 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!
    Jun 27 '08 #8
    Ian Collins <ia******@hotmail.comwrote in news:66am3lF2jfs1fU6
    @mid.individual.net:
    Rob wrote:
    >>
    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.
    Overloaded methods in general, yes, but here the issue is with different
    template instantiations. These work fine with the same argument types
    (unless one is using MS Visual C++ 6.0, which predates the standard and is
    obsolete now for many years already).

    Best!
    Paavo
    Jun 27 '08 #9
    On 4ÔÂ12ÈÕ, ÏÂÎç11ʱ52·Ö, Rob <someidunknown1...@yahoo.comwrote:
    On Apr 12, 9:24 am, Davis King <davis...@gmail.comwrote:


    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.comwrote:
    Rob wrote:
    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.
    --
    Ian Collins.
    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<Tget(int c)
    {
    return vector<T>();
    }
    int main()
    {
    vector<vector<string ret1 = get<vector<string( 0 );
    vector<stringret2 = 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 );
    Is this you want?

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

    int main()
    {
    std::vector<std::vector<std::stringret = get<std::string>( c );
    }
    Jun 27 '08 #10

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    0
    1652
    by: CoreyMas | last post by:
    Hello Everyone, I have been successful in creating a template column programatically using the examples provided in VS 2003. However I have not been able to programatically set the width of a template column programatically Here is the code that I have used so far
    17
    2016
    by: Jacek Dziedzic | last post by:
    Hello! I have a templated class that serves as a simple vector of elements. template <typename T> class simple_vector : public math_object { // ... lots of simple_vector operations // the trouble begins here:
    3
    2080
    by: learning_C++ | last post by:
    I hope to use template. in my code I hope to sum the vector<int> and vector<double>. But there are several errors: sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no type sumtemplate.cpp: In function `int sum(std::vector<T, std::allocator<_CharT> >&) ': sumtemplate.cpp:7: warning: `std::vector<T, std::allocator<_CharT>...
    2
    4535
    by: Nicole | last post by:
    I am creating template columns programmatically. I have read the msdn article on this and I'm so close. Article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingwebservercontroltemplatesprogrammatically.asp I have narrowed down the problem comes in when the _DataBinding handler is called and...
    0
    1982
    by: Pat Sagaser via .NET 247 | last post by:
    I'm trying to add LinkButtons to a Repeater control using adynamic template. The docs state that you should be able tobubble the click event to the containing Repeater. There areplenty of examples in the documentation for doing this using an<ItemTemplate> tag, but I haven't found any indication for howyou would do this in a dynamic template...
    4
    7351
    by: Damien | last post by:
    Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument, and the compiler gives me an invalid template argument on the member function address if the member function returns a type. A member function with a...
    3
    3740
    by: Hamilton Woods | last post by:
    Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
    0
    1184
    by: imranabdulaziz | last post by:
    Hi all, I am using asp.net and C# Visual studio 2005. Let me explain the scenario. I have stored procedure which return very no of column based on condition. Becoz I have to show columnwise record I used datalist. And becoz of query return different no of column based on condtion I created template class for design and itembinding at run...
    2
    1387
    by: .rhavin grobert | last post by:
    hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function... ______________________________________ template <class T, typename I>
    0
    7276
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
    0
    7408
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
    0
    7581
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
    0
    7548
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
    0
    5714
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
    0
    4773
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
    0
    3267
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
    0
    3259
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    0
    488
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.