| 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
.
|
|
|
|

|
Related Articles |
|
|