473,671 Members | 2,384 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 2590
On Apr 11, 9:38 pm, Rob <someidunknown1 ...@yahoo.comwr ote:
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::s tring 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.comwr ote:
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::s tring 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.co mwrote:
    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.comwr ote:
    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.co mwrote:
    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<_Ty pe1>&
    vect ) {
    // Stuff

    return vect;
    }

    template <class _Type2void get( SomeType c, std::vector<_Ty pe2>&
    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.co mwrote:
    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<s tring ret1 = get<vector<stri ng( 0 );
    vector<stringre t2 = 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.co mwrote:
    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<s tring ret1 = get<vector<stri ng( 0 );
    * * vector<stringre t2 = 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<s tring ret = get<vector<stri ng( c );

    when it should have been:

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

    Thanks!
    Jun 27 '08 #8
    Ian Collins <ia******@hotma il.comwrote in news:66am3lF2jf s1fU6
    @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.comwr ote:
    On Apr 12, 9:24 am, Davis King <davis...@gmail .comwrote:


    On Apr 11, 10:46 pm, Ian Collins <ian-n...@hotmail.co mwrote:
    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<s tring ret1 = get<vector<stri ng( 0 );
    vector<stringre t2 = 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<s tring ret = get<vector<stri ng( c );

    when it should have been:

    vector<vector<s tring 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::s tringret = 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
    1662
    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
    2030
    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
    2089
    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> >::iterator'
    2
    4549
    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 the literal text is being assigned, but I can't figure out beyond that. The main problem is that...
    0
    1993
    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 (implementing theITemplate interface). I'm adding the LInkButton in the TemplateDataBinding...
    4
    7372
    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 void return type is fine. The example below demonstrates the problem:
    3
    3750
    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. I built a console app using Borland C++ Builder 5. The linker complained of references to...
    0
    1189
    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 time . I manage to create template column() but confuse over how to bind data. As no of column...
    2
    1398
    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
    8478
    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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
    0
    8397
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
    0
    8919
    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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
    0
    8821
    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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
    1
    8599
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
    0
    4409
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    1
    2813
    by: 6302768590 | last post by:
    Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
    2
    2052
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.
    2
    1810
    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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

    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.