Need template work-around in VC6

  • Thread starter Thread starter mlimber
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 

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
 
Back
Top