Advertisement

using C++ templates

Started by December 26, 2000 02:35 PM
4 comments, last by yossarian 24 years ago
can anyone tell me why the following doesn't compile in gcc 2.9.5: class t1 { public: template void f() { } }; template void function() { t1 t; t.f(); // <-- line 16 } int main() { function(); return 0; } i get a 'parse error before , ' on line 16 the program compiles if function() is not a template Edited by - yossarian on 12/26/00 2:44:11 PM
--this line is blank--
also, why are sections in angled brackets not displayed??
it might help to see them in the above code
--this line is blank--
Advertisement
I believe > and < is done by &_gt and &_lt without the underscore(_).

YAP-YFIO,
deadlinegrunt

~deadlinegrunt

The code looks fine to me, and it compiles ok in Borland C++. MSVC doesn''t like it: "error C2062: type ''int'' unexpected" on the line marked as 16. I can''t tell why it''s breaking though, and I don''t have gcc. But it looks like it''s the compiler that''s wrong, not you

btw when code doesn''t show up properly because of templates, you can use the edit post button to see it in plain text.

As deadlinegrunt said, you need replace all < with &lt and > with
&gt to get this:

class t1
{
public:
template <class a, class b> void f() { }
};


template <class a, class b> void function()
{
t1 t;
t.f<int,int>(); // <-- line 16
}

int main()
{
function<int,int>();
return 0;
}
Thanks for that little tip there Wilka! I didn't realize you could do that. (To think I tried to view the source of the HTML, doh!)

Now please keep this in mind, I am not a guru on this subject:

It's my understanding that a template-declaration is a declaration and that it may also only appear in the global namespace. Now from the standard I'm aware of, certain situations could exists where you could nest template declarations and it would be feasible where as others would not. It's my understanding also that the standard determined that it was better to make a general rule regarding this rather than a rule with general exceptions.

So my point is this:
    template<class a, class b>class t1{a _a; b _b; // f() uses and acts on these members perhaps?public:   void f() { } // not template<class a, class b> void f() {} };    


Wait and see if someone more versed on this subject comes along and rips my thoughts apart before you run with it though.




YAP-YFIO,
deadlinegrunt

P.S. Not all compilers conform to the ANSI/ISO standard so that's not to say that you won't/can't see code that deviates from this and still work. Templates are another area where all compilers aren't exactly equal in implementation.

Edited by - deadlinegrunt on December 27, 2000 12:50:26 PM

~deadlinegrunt

hmmmm, thanks for the help guys

i should probably go away and study the c++ standard for a while, till i understand how templates work properly..

--this line is blank--

This topic is closed to new replies.

Advertisement