I want to do something like this: -
template <typename MyType>
-
std::vector<MYTypeget(SomeClass c)
-
{
-
...elided...
-
}
-
-
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? 9 2590
On Apr 11, 9:38 pm, Rob <someidunknown1 ...@yahoo.comwr ote:
I want to do something like this:
-
template <typename MyType>
-
std::vector<MYTypeget(SomeClass c)
-
{
-
...elided...
-
}
-
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
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:
-
template <typename MyType>
-
std::vector<MYTypeget(SomeClass c)
-
{
-
* * *...elided...
-
-
}
-
-
-
-
-
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?
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.
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!
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
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
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!
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
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 );
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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:
|
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'
|
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...
|
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...
| |
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:
|
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...
|
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...
|
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>
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |