By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

Template member functions

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ulrich Heinen wrote in news:bp**********@n.ruf.uni-freiburg.de:
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< int >();
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
Ulrich Heinen wrote:
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:

m.test<some_type>();
return EXIT_SUCCESS;
}


Jul 22 '05 #3

P: n/a
Rob Williscroft wrote:
Ulrich Heinen wrote in news:bp**********@n.ruf.uni-freiburg.de:
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< int >();
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.


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.