Parse Error in template class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave O'Hearn

    Parse Error in template class

    This is a stripped down example that duplicates the problem I am
    having. If I do it with concrete types, I can access the set's
    iterator type correctly. If I make a template with generic types, I
    can't get it right.

    #include <set>

    // concrete version. this compiles fine.
    class it_works
    {
    private:
    typedef std::pair<int, double> pair_t;
    typedef std::set<pair_t > set_t;

    set_t my_set;

    public:
    void func() {
    set_t::iterator i;
    std::set<pair_t >::iterator j;
    std::set< std::pair<int, double> >::iterator k;
    }
    };

    // generic version, templated on T1 and T2. it doesn't work.
    template<typena me T1, typename T2>
    class doesnt_work
    {
    private:
    typedef std::pair<T1, T2> pair_t;
    typedef std::set<pair_t > set_t;

    set_t my_set;

    public:
    void func() {
    set_t::iterator i; // "parse error before `;' token"
    std::set<pair_t >::iterator j; // same error
    std::set< std::pair<T1, T2> >::iterator k; // same error
    }
    };

    It seems to be just a stupid syntax error. If I replace one of the
    lines with "BLAH something = _abcdefg_;", I get the same error. But I
    can't figure out why I get the parsing errors on the generic version,
    but not the concrete version. Testing a concrete version is usually
    the process I go through to track down parse errors in a template, but
    it didn't throw any light on things this time.

    If it helps, I am using gcc-3.2.2, but I am fairly sure it is just a
    syntax error.

    --
    Dave O'Hearn
  • Dave O'Hearn

    #2
    Re: Parse Error in template class

    Gianni Mariani <gi2nospam@mari ani.ws> wrote:[color=blue]
    > In a template it's sometimes impossible to tell if the token you are
    > about to parse is part of a type declaration or an identifier because it
    > is incomplete (by definition). To get around this, the keyword
    > "typename" is used to indicate to the compiler what it is.[/color]

    Thanks. I had heard stories about the need to occasionally throw
    "typename" around, but I figured it only happened in very weird
    meta-template code and it would be years before I had to learn what it
    meant. I guess not!
    [color=blue]
    > gcc 3.2.2 has some (many?) template parsing bugs. I used gcc 3.3 and
    > the messages were pretty good.[/color]

    I am waiting for 3.4... just a few more months now!

    --
    Dave O'Hearn

    Comment

    • Dave O'Hearn

      #3
      Re: Parse Error in template class

      Gianni Mariani <gi2nospam@mari ani.ws> wrote:[color=blue]
      > In a template it's sometimes impossible to tell if the token you are
      > about to parse is part of a type declaration or an identifier because it
      > is incomplete (by definition). To get around this, the keyword
      > "typename" is used to indicate to the compiler what it is.[/color]

      Thanks. I had heard stories about the need to occasionally throw
      "typename" around, but I figured it only happened in very weird
      meta-template code and it would be years before I had to learn what it
      meant. I guess not!
      [color=blue]
      > gcc 3.2.2 has some (many?) template parsing bugs. I used gcc 3.3 and
      > the messages were pretty good.[/color]

      I am waiting for 3.4... just a few more months now!

      --
      Dave O'Hearn

      Comment

      Working...