Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top