S
sunil
Hello,
I have a template class as follows which has a static function called
foo, as you see below the class declaration, there are specializations
for foo for different specializations of T, the problem I have is I
cannot seem to get the syntax right for invoking foo() (since foo()
is static member, I should be able to invoke the foo() on specialized
class (no need for object)), if I uncomment first two lines in main()
code it fails to compile, if its commented it complies and works as
expected (note that bi.foo() and bf.foo() work properly).
compiling this code on SUNOS, version of CC: CC: Sun C++ 5.5 Patch
113817-12 2005/03/08
any help greately appreciated.
Thanks,
Sunil
#include "temp1.h"
typedef b<int,COLOR_GREEN> BINT;
typedef b<float,COLOR_RED> BFLOAT;
void main()
{
//BINT.foo();
//BFLOAT.foo();
BINT bi(10);
BFLOAT bf(12.2);
bi.print();
bf.print();
bi.foo();
bf.foo();
}
temp1.h:
#include <iostream>
using namespace std;
typedef enum {
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE
}ColorType;
template <class T,ColorType color>
class b
{
public:
b(T t): _data(t) {
}
~b() {
}
static void foo();
void print() {
cout << "Value=" << _data << endl;
switch(color) {
case COLOR_RED:
cout << "specialized with RED color" << endl;
break;
case COLOR_GREEN:
cout << "specialized with GREEN color" << endl;
break;
case COLOR_BLUE:
cout << "specialized with BLUE color" << endl;
break;
}
}
private:
T _data;
} ;
template <>
void
b<int,COLOR_GREEN>::foo() {
cout << "foo called with instantiation int,COLOR_GREEN" << endl;
}
template <>
void
b<float,COLOR_RED>::foo() {
cout << "foo called with instantiation float,COLOR_RED" << endl;
}
I have a template class as follows which has a static function called
foo, as you see below the class declaration, there are specializations
for foo for different specializations of T, the problem I have is I
cannot seem to get the syntax right for invoking foo() (since foo()
is static member, I should be able to invoke the foo() on specialized
class (no need for object)), if I uncomment first two lines in main()
code it fails to compile, if its commented it complies and works as
expected (note that bi.foo() and bf.foo() work properly).
compiling this code on SUNOS, version of CC: CC: Sun C++ 5.5 Patch
113817-12 2005/03/08
any help greately appreciated.
Thanks,
Sunil
#include "temp1.h"
typedef b<int,COLOR_GREEN> BINT;
typedef b<float,COLOR_RED> BFLOAT;
void main()
{
//BINT.foo();
//BFLOAT.foo();
BINT bi(10);
BFLOAT bf(12.2);
bi.print();
bf.print();
bi.foo();
bf.foo();
}
temp1.h:
#include <iostream>
using namespace std;
typedef enum {
COLOR_RED,
COLOR_GREEN,
COLOR_BLUE
}ColorType;
template <class T,ColorType color>
class b
{
public:
b(T t): _data(t) {
}
~b() {
}
static void foo();
void print() {
cout << "Value=" << _data << endl;
switch(color) {
case COLOR_RED:
cout << "specialized with RED color" << endl;
break;
case COLOR_GREEN:
cout << "specialized with GREEN color" << endl;
break;
case COLOR_BLUE:
cout << "specialized with BLUE color" << endl;
break;
}
}
private:
T _data;
} ;
template <>
void
b<int,COLOR_GREEN>::foo() {
cout << "foo called with instantiation int,COLOR_GREEN" << endl;
}
template <>
void
b<float,COLOR_RED>::foo() {
cout << "foo called with instantiation float,COLOR_RED" << endl;
}