Hello, all.
I should lke to declare template function as following:
template <class TYPE>
TYPE Test() {return (TYPE)5; }
void main(void) {
int i; float f;
i = Func();
f = Func();
}
What is wrong with this?
What I receive from gcc is :
main.cpp:28: no matching function for call to `Func()'
Thanks ahead
Leon 6 15659
PLM schreef: Hello, all. I should lke to declare template function as following:
template <class TYPE> TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
void main(void) { int i; float f; i = Func(); f = Func(); }
What is wrong with this? What I receive from gcc is :
main.cpp:28: no matching function for call to `Func()'
The definition is completely legal. However, you must specify
TYPE. You simply cannot deduce TYPE from the =, because the overload
of operator= is selected based on TYPE. C++ resolves these things
from the bottom up. One has to choose a direction when breaking
such logical cycles.
So, write i = Func<int>(); or
template <class TYPE>
void Func( TYPE& t ) { t = 5; }
Func( i );
HTH,
Michiel Salters
Hi,
of course, I suppose that you meant to call Test(), since Func() is not
declared.
Template matching is based on function overloading matching.
Function overloading doesn't work for return types, but only for
parameter's types.
You have to specify the template specialization like this:
i = Func<int>();
Ciao,
Mario Fratelli.
msalters wrote: PLM schreef:
Hello, all. I should lke to declare template function as following:
template <class TYPE> TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
void main(void) { int i; float f; i = Func(); f = Func(); }
What is wrong with this? What I receive from gcc is :
main.cpp:28: no matching function for call to `Func()'
The definition is completely legal. However, you must specify TYPE. You simply cannot deduce TYPE from the =, because the overload of operator= is selected based on TYPE. C++ resolves these things from the bottom up. One has to choose a direction when breaking such logical cycles.
So, write i = Func<int>(); or template <class TYPE> void Func( TYPE& t ) { t = 5; } Func( i );
HTH, Michiel Salters
Thank you for reply.
Yes, I understand that i = Func<int>() will work, but this is exactly I
wanted to avoid...:-))
Now, please, can you be so kind to detail your explanation about the
direction? I did not catch...:-((
> Yes, I understand that i = Func<int>() will work, but this is exactly I wanted to avoid...:-))
Why do you want to avoid it? If Func is to return an object of some type,
then you need to specify what that type is. It is only when that is
determined the compiler would worry about how to convert the return type for
the assignment operation.
One way to work around is to not use the return value mechanism. Pass the
variable by reference:
template <typename T>
Func(T& t)
{
t = T(5);
}
Now, please, can you be so kind to detail your explanation about the direction? I did not catch...:-((
Consider:
class A{};
class B{};
A& operator = (A&, const B&);
B& operator = (B&, const A&);
template <typename T>
T Func(void);
A a;
a = Func(); // problem line
In the last line (commented problem line), the compiler has serveral
options, neither is much better than the other and thus would raise
ambiguity. In fact, the compiler never knows ahead about the conversion so
it simply can't depend on the return type to match functions (and function
templates).
"PLM" <pl*@iname.com> wrote in message
news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@t eranews... Hello, all. I should lke to declare template function as following:
template <class TYPE> TYPE Test() {return (TYPE)5; }
void main(void) {
int main(void) {
int i; float f; i = Func(); f = Func();'
i = Test<int>();
f = Test<float>();
-Mike
I think template cast operator would help...
Create a small class which encapsulates value of 5:
static class {
public:
template<typename T>
operator T() { return static_cast<T>(5); }
} five = {};
#include <iostream>
using std::cout;
using std::endl;
int main() {
float valuef = five;
int valuei = five;
cout << valuef << endl;
cout << valuei << endl;
return 0;
}
Hope that helps!
Martin
"PLM" <pl*@iname.com> wrote in message
news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@t eranews... Hello, all. I should lke to declare template function as following:
template <class TYPE> TYPE Test() {return (TYPE)5; }
void main(void) { int i; float f; i = Func(); f = Func(); }
What is wrong with this? What I receive from gcc is :
main.cpp:28: no matching function for call to `Func()'
Thanks ahead Leon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matt Graham |
last post by:
ok, I have 2 classes roughly like these 2:
class A; // various data classes to be used in D
class B {
public:
void x() { y() }
protected:
virtual void y();
};
|
by: claude uq |
last post by:
Hello,
Am trying to use template functions within some class to convert int, float,
doubles, etc into strings.
Below, three ways to do it via use of "to_string(const T & Value)" . The
only...
|
by: mast2as |
last post by:
Is it possible to limit a template class to certain types only. I found
a few things on the net but nothing seems to apply at compile time.
template <typename T>
class AClass
{
public:...
|
by: Markus Petermann |
last post by:
Hello,
I have a small problem I want to demonstrate with a small demo program:
-------------- Snip --------------
#include <string>
namespace
{
|
by: chrisstankevitz |
last post by:
Apparently default template arguments can only be used in classes. I
include two apps below. One works but is IMO messy. The other does
not work and is IMO clean.
Q1: Why would c++ dissallow...
|
by: Kith Kanan |
last post by:
In a recent thread "Why is overloading operator. (member operator)
forbidden", someone wrote a code like the following:
template <typename R, typename... Argtypes>
class gen_send {
private:...
|
by: =?ISO-8859-1?Q?Andr=E9_Luiz_Carvalho?= |
last post by:
Hi there,
I'm porting an application from Java to C++ / BREW so I can't use the
stl, therefore, I have to implement the basic structures that the app
use in Java. I'm implementing a Hashtable...
|
by: Ajb181 |
last post by:
hi all. i have a template class that i can not link. there is no problem when i add the method def's inside the header file but as soon as i move them out i get a link error where i define the...
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |