Need template work-around in VC6

M

mlimber

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
 
C

Carl Daniel [VC++ MVP]

mlimber said:
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?

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
 
M

mlimber

Carl said:
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
 
T

Tom Widmer [VC++ MVP]

mlimber said:
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
 
M

mlimber

Tom said:
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
 
B

Bo Persson

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

Cheers! --M

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


Bo Persson
 
B

Bo Persson


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
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top