Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function template problem

Status
Not open for further replies.

globos

Programmer
Joined
Nov 8, 2000
Messages
260
Location
FR
Hi,

I want to use function template, and did something like this :
class Test
{
public:
template<class T>
void foo ()
{
}
};

int main ()
{
Test t;

t.foo<int> ();
return 0;
}

When compiling with VC++ 6.0, I get :
error C2062: type 'int' unexpected
But there are no compilation errors with g++.
Apparently, VC++ compiler does not understand explicit function template arguments.
How can I work around this problem?

--
Globos
 
use a formal default parameter.

class Test
{
public:
void foo (template<class T>)
{
}
};

int main ()
{
Test t;

t.foo(int(0));
return 0;
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thanks, it works(in your sample &quot;template&quot; def is not at the right place).
The compiler instantiates the appropriate function from the type of the actual parameter.
Does the VC++ .NET compiler accepts explicit function template arguments now?

--
Globos
 
I do not believe

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
It should; I've used it before on 6.0, but on functions with more than 0 arguments. I think maybe it was getting confused since you had a 0-argument function template. Such a thing makes perfect sense, and if it's not Standard it should be, but you gotta admit you don't see it every day, so maybe it got past MS's testers. I don't know if they've fixed that hiccup by now or not.
 
I do not believe what it is a bug. Just Microsoft has not considered to introduce this feature.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
But why would they choose to implement it for functions with 1+ arguments but not with 0 arguments?

I took a quick look at the C++ Standard (well, the draft) and found no reason that 0-argument functions should be an exception.

I'd call that a bug. In Standard-compliance if nothing else.


If they didn't &quot;consider to introduce&quot; it, that means they forgot about it. Since it's part of the specification for C++ compilers, it should be there.

If I tell you to write a program that does X, Y, and Z, and you write a program that does X and Y, but you forget to do Z, you can't tell me the program is fine because you didn't &quot;consider to introduce&quot; Z.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top