Template member functions

U

Ulrich Heinen

Hello,

I'm having trouble with a template member function in a non template class.
The attached sample program compiles and works just fine with GCC or IBM
VisualAge compilers but Sun Workshop 6 (C++ 5.1) doesn't like it. Am I
doing something illegal that accidentally works on the two first compilers
or is it the Sun compiler that gets it wrong?
The sun compiler stumbles over the indicated line with the message "Badly
formed expression."

Can anybody point me in the right direction? Thanks!

Ulrich

#include <cstdlib>
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class A {
public:
A(const char * name) : _name(name) {}
template<typename T> void test() const {
cout << _name << ": "<< typeid(T).name() << endl;
}
private:
string _name;
};

int main(int /*argc*/, char ** /*argv*/) {
A m("Hello");
m.test(); // this is the line the Sun compiler stumbles over
return EXIT_SUCCESS;
}
 
R

Rob Williscroft

Ulrich Heinen wrote in
Hello,

I'm having trouble with a template member function in a non template
class. The attached sample program compiles and works just fine with
GCC or IBM VisualAge compilers but Sun Workshop 6 (C++ 5.1) doesn't
like it. Am I doing something illegal that accidentally works on the
two first compilers or is it the Sun compiler that gets it wrong?
The sun compiler stumbles over the indicated line with the message
"Badly formed expression."

Can anybody point me in the right direction? Thanks!

Ulrich

#include <cstdlib>
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class A {
public:
A(const char * name) : _name(name) {}
template<typename T> void test() const {
cout << _name << ": "<< typeid(T).name() <<
endl;
}
private:
string _name;
};

int main(int /*argc*/, char ** /*argv*/) {
A m("Hello");
m.test(); // this is the line the Sun compiler stumbles over

this should be:

m.test said:
return EXIT_SUCCESS;
}

Note that MS Visual C++ 7.1 and gcc 3.2.3 (MinGW) both reject your
original code. I really can't imagine what your GCC or IBM VisualAge
compilers are doing when they compile this.

Rob.
 
U

Ulrich Heinen

Rob said:
Ulrich Heinen wrote in

this should be:



Note that MS Visual C++ 7.1 and gcc 3.2.3 (MinGW) both reject your
original code. I really can't imagine what your GCC or IBM VisualAge
compilers are doing when they compile this.

Rob.

You're of course right - I messed up the posting while trying to get the
code compile somehow on the sun. The intended code was indeed as you write,
and then works on Linux/GCC and AIX/VisualAge - only the sun compiler
rejects it. Seems it is a compiler bug.

Thanks for your help!

Ulrich
 
R

Rolf Magnus

Ulrich said:
Hello,

I'm having trouble with a template member function in a non template
class. The attached sample program compiles and works just fine with
GCC or IBM VisualAge compilers

I can't believe that. Which gcc version did you try?
but Sun Workshop 6 (C++ 5.1) doesn't like it. Am I doing something
illegal that accidentally works on the two first compilers or is it
the Sun compiler that gets it wrong?

You're trying to call a function template directly. You have to specify
the template type for calling the function. How would the compiler be
able to figure that out itself?
The sun compiler stumbles over the indicated line with the message
"Badly formed expression."

Can anybody point me in the right direction? Thanks!

Ulrich

#include <cstdlib>
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class A {
public:
A(const char * name) : _name(name) {}
template<typename T> void test() const {
cout << _name << ": "<< typeid(T).name() <<
endl;
}
private:
string _name;
};

int main(int /*argc*/, char ** /*argv*/) {
A m("Hello");
m.test(); // this is the line the Sun compiler stumbles over

This call has to be in the form:
 

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

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top