implicit typename in template



 DEVELOP > c-Plus-Plus > implicit typename in template

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Steve Hill"
Date: 06 Sep 2003 02:01:33 AM
Object: implicit typename in template
Hi,
When compiling under g++ (version 3.2) the following code fragement gives me a
warning:
steve@khan:~/tmp> /opt/bin/g++ -o state_test state_test.cpp
state_test.cpp:42: warning: `typename State<T>::State_change_counter' is
implicitly a typename
state_test.cpp:42: warning: implicit typename is deprecated, please see the
documentation for details
Any ideas why, and how to avoid it?
Best regards
Steve
#include <map>
#include <iterator>
using namespace std;
template <class T>
class State
{
public:
State(T initial_state=T(0))
:state(initial_state)
{};

virtual ~State()
{};
T operator()() const
{ return (T)state; };

void operator=(const T new_state)
{
++state_changes[State_change((T)state, new_state)];
state=(unsigned int)(new_state);
};
bool operator==(const T comp_state) const
{ return state==(unsigned int)(comp_state); };

private:
unsigned int state;

typedef pair<T,T> State_change;
typedef map<State_change, int> State_change_counter;
static State_change_counter state_changes;
};
template <class T>
State<T>::State_change_counter State<T>::state_changes; // WARNING HERE
enum E{ A,B, C };
int main (void)
{
State<E> s;
s = B;
return 0;
}
.

User: "ES Kim"

Title: Re: implicit typename in template 06 Sep 2003 02:52:51 AM
"Steve Hill" <stephen.hill@motorola.com> wrote in message
news:c230c758.0309052301.69400be5@posting.google.com...

Hi,
When compiling under g++ (version 3.2) the following code fragement gives me

a

warning:
steve@khan:~/tmp> /opt/bin/g++ -o state_test state_test.cpp
state_test.cpp:42: warning: `typename State<T>::State_change_counter' is
implicitly a typename
state_test.cpp:42: warning: implicit typename is deprecated, please see the
documentation for details

Any ideas why, and how to avoid it?

Best regards

Steve


#include <map>
#include <iterator>
using namespace std;

template <class T>
class State
{
public:
State(T initial_state=T(0))
:state(initial_state)
{};

virtual ~State()
{};

T operator()() const
{ return (T)state; };


void operator=(const T new_state)
{
++state_changes[State_change((T)state, new_state)];
state=(unsigned int)(new_state);
};

bool operator==(const T comp_state) const
{ return state==(unsigned int)(comp_state); };

private:
unsigned int state;

typedef pair<T,T> State_change;
typedef map<State_change, int> State_change_counter;
static State_change_counter state_changes;
};

template <class T>
State<T>::State_change_counter State<T>::state_changes; // WARNING HERE

typename State<T>::State_change_counter State<T>::state_changes;
It's a hint for the complier that State<T>::State_change_counter is
the name of a type, not a variable or something else. Whenever you
use a name as a type that depends on template parameter(T in this case),
you must precede the name by "typename" to let the compiler know
to treat the the name as a type.


enum E{ A,B, C };

int main (void)
{
State<E> s;
s = B;

return 0;
}

--
ES Kim
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER