Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

Need template work-around in VC6

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mlimber - 23 Aug 2006 19:33 GMT
I am using VC++ 6 (sp6) and cannot change compilers. I have something
akin to this code:

#include <iostream>
using namespace std;

template<int i>
struct Int2Type
{
    enum { value = i };
};

struct Foo
{
    template<int i>
    void Bar()
    {
        cout << i << endl;
    }

    // Ugly work-around
    template<int i>
    void Baz( const Int2Type<i>& )
    {
        cout << i << endl;
    }
};

int main()
{
    Foo f;
    f.Bar<42>(); // Error here
    f.Baz( Int2Type<42>() );
    return 0;
}

The error message is: "error C2059: syntax error : ')'". I can use the
ugly work-around with Int2Type that is shown, but I would much prefer
something simpler and more aesthetically pleasing. Any ideas?

Cheers! --M
Carl Daniel [VC++ MVP] - 24 Aug 2006 00:41 GMT
>I am using VC++ 6 (sp6) and cannot change compilers. I have something
> akin to this code:
[quoted text clipped - 35 lines]
> ugly work-around with Int2Type that is shown, but I would much prefer
> something simpler and more aesthetically pleasing. Any ideas?

For VC6?  No, not really (not that I've seen anyway).  The parser simply
doesn't understand explicit template arguments on a function invocation.

-cd
mlimber - 24 Aug 2006 07:14 GMT
> >I am using VC++ 6 (sp6) and cannot change compilers. I have something
> > akin to this code:
[quoted text clipped - 38 lines]
> For VC6?  No, not really (not that I've seen anyway).  The parser simply
> doesn't understand explicit template arguments on a function invocation.

Except for free-standing functions, of course. Thanks for responding!

Cheers! --M
Tom Widmer [VC++ MVP] - 25 Aug 2006 10:32 GMT
>> For VC6?  No, not really (not that I've seen anyway).  The parser simply
>> doesn't understand explicit template arguments on a function invocation.
>
> Except for free-standing functions, of course. Thanks for responding!

Right - the member function template support is extremely patchy. And
for free standing functions, you have to make sure that T is present
somewhere in the function signature in any case (e.g.
template <class T>
void f(T* t = 0); //default parameter to make sure VC6 doesn't
//collapse all instantiations into one.
//f<int>() should work though, IIRC.

Any reason why you're using an 8 year old compiler? That would be like
still using egcs rather than GCC!

Tom
mlimber - 25 Aug 2006 14:40 GMT
> Any reason why you're using an 8 year old compiler? That would be like
> still using egcs rather than GCC!

Because I'm working with legacy code, and the management wants to avoid
surprises. Me, I like surprises. :-)

Cheers! --M
Bo Persson - 25 Aug 2006 16:28 GMT
>> Any reason why you're using an 8 year old compiler? That would be
>> like
[quoted text clipped - 5 lines]
>
> Cheers! --M

And the fact that it doesn't work is no surprise. Then what?  :-)

Bo Persson
Bo Persson - 25 Aug 2006 16:34 GMT
>>> For VC6?  No, not really (not that I've seen anyway).  The parser
>>> simply
[quoted text clipped - 3 lines]
>> Except for free-standing functions, of course. Thanks for
>> responding!

It just looks that way, until you try to use several different
instantiations!

> Right - the member function template support is extremely patchy.
> And for free standing functions, you have to make sure that T is
[quoted text clipped - 3 lines]
> //collapse all instantiations into one.
> //f<int>() should work though, IIRC.

The compiler actually works (almost :-) correctly for the free
functions, in that it produces one set of code for each instantiation.
Unfortunately they all have the same signature,
void-f-with-no-parameters.

Then, of course, the linker can't tell them apart so we don't know
which one we get.

> Any reason why you're using an 8 year old compiler? That would be
> like still using egcs rather than GCC!

Right.

"To avoid surprises".  :-)

Bo Persson

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.