implicit typename in template

Discussion in 'C++' started by Steve Hill, Sep 6, 2003.

  1. Steve Hill

    Steve Hill Guest

    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;
    }
     
    Steve Hill, Sep 6, 2003
    #1
    1. Advertising

  2. Steve Hill

    ES Kim Guest

    "Steve Hill" <> wrote in message
    news:...
    > 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
     
    ES Kim, Sep 6, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kalicinski

    Implicit typename

    Marcin Kalicinski, Oct 1, 2004, in forum: C++
    Replies:
    1
    Views:
    356
    Mike Wahler
    Oct 1, 2004
  2. =?windows-1252?Q?=8Aimon_T=F3th?=

    Implicit typename warning (g++ linux)

    =?windows-1252?Q?=8Aimon_T=F3th?=, Apr 27, 2005, in forum: C++
    Replies:
    3
    Views:
    1,363
    Victor Bazarov
    Apr 27, 2005
  3. lutorm
    Replies:
    1
    Views:
    2,194
    Rapscallion
    May 21, 2005
  4. Generic Usenet Account
    Replies:
    3
    Views:
    828
    Generic Usenet Account
    Jul 14, 2005
  5. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    356
    Rolf Magnus
    Mar 14, 2006
Loading...

Share This Page