views:

36

answers:

3

Hello all, I think I've run into a (possible) VC6 (I know. It's what we use.) compiler error, but am open to the fact that I've just missed something dumb. Given the following code (It's just an example!):

#include <iostream>

// Class with template member function:
class SomeClass
{
public:
  SomeClass() {};

  template<class T>
  T getItem()
  {
    return T();
  };
};


// Dummy just used to recreate compiler error
class OtherClass
{
public:
  OtherClass() {};
};

std::ostream& operator<<( std::ostream& oStr, const OtherClass& obj )
{
  return oStr << "OtherClass!";
};

// Main illustrates the error:
int main(int argc, char* argv[])
{
  SomeClass a;

  OtherClass inst2 = a.getItem<OtherClass>(); // Error C2275 happens here!
  std::cout << inst2 << std::endl;

  return 0;
}

If I try to compile this code VC6, dies on a.getItem<OtherClass>() yielding:

Error C2275: 'OtherClass' : illegal use of this type as an expression.

Have I overlooked some trivial syntax issue? Am I breaking a rule? This code compiles just fine under gcc 4.3.4. Is it yet another compliance issue with VC6?

Thanks!

+1  A: 

This is likely to be a VC6 issue. Although VC6 compiles most basic templates correctly it is known to have many issues when you start to move towards the more advanced template uses. Member templates are an area where VC6 is known to be weak on conformance.

Charles Bailey
I'll take this as the correct answer unless someone shows up and can say otherwise!
acanaday
+1  A: 

I believe that's another bug in VC6, you should really switch to a more up-to-date compiler.

Matias Valdenegro
_I_ would **love** to. Unfortunately, I can't.
acanaday
http://www.cplusplus.com/forum/general/11928/ Try this.
Matias Valdenegro
Then you need to use a non-templated solution :(
Goz
Yeah, I am already using another scheme that avoids this problem entirely. I was just curious to know if the problem was me or VC6.
acanaday
@Goz The solution i see in the link is still templated.
Matias Valdenegro
@Matias thanks for the link! In my case, the solution presented there is not quite applicable to my actual problem. The example I have here is the result of boiling everything away until I found the one line which caused the error. I've got a working solution anyway. _Thanks!_
acanaday
@acanaday: In general, when VC6 complains and there's a `template` involved somewhere, assume it's the compiler. (And that's coming from me, who's always preached that 99% of all reported "compiler errors" actually are user errors.)
sbi
+1  A: 

Among many other things with the word template in it, VC6 couldn't deal with function templates where the template parameters aren't also function parameters. The common workaround was to add a dummy function parameter:

  template<class T>
  T getItem(T* /*dummy*/ = NULL)
  {
    return T();
  } // note: no ; after function definitions

However, in general, VC6 is pretty lame and often chokes as soon as a TU contains the template keyword. I had to beat my head against it for several years (big code base compiled with several compilers/compiler versions; VC6 giving us an endless amount of trouble) and was very glad when I got rid of it in 2003.

sbi