implicit typename in template

S

Steve Hill

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;
}
 
E

ES Kim

Steve Hill said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top