Re: template specification oddness

Discussion in 'C++' started by Victor Bazarov, Feb 21, 2008.

  1. Matthias Buelow wrote:
    > Hi folks,
    >
    > I have a weird problem that I can't seem to put my finger on. The
    > following example program illustrates it:
    >
    > ----------------------------------------------------------------------
    > using namespace std;
    >
    > template<typename Tval, typename Targ> struct Closure {
    > virtual Tval f(Targ arg) = 0;
    > virtual ~Closure() = 0;
    > // add environment by subclassing
    > };
    >
    > #include <list>
    >
    > template<typename Targ> struct Hooks {
    > std::list<Closure<void, Targ> *> hooks;
    >
    > void AddHook(Closure<void, Targ> *cl) { hooks.push_front(cl); }
    > void RemoveHook(Closure<void, Targ> *cl) { hooks.remove(cl); }
    > void RunHooks(Targ arg) {
    > std::list<Closure<void, Targ> *>::const_iterator i =


    typename std::list<Closure<void, Targ> *>::const_iterator i =

    > hooks.begin(); for (; i != hooks.end(); i++)


    ... ; ++i)

    > (*i)->f(arg);
    > }
    > };
    >
    > int main()
    > {
    > Hooks<bool> h;
    >
    > return 0;
    > }
    > ----------------------------------------------------------------------
    >
    > When compiling this, g++ (g++ (GCC) 4.1.2 20061115 (prerelease)
    > (Debian 4.1.1-21)) gives the following error:
    >
    > t.cc: In member function 'void Hooks<Targ>::RunHooks(Targ)':
    > t.cc:17: error: expected `;' before 'i'
    > t.cc:18: error: 'i' was not declared in this scope
    >
    > The Intel compiler, however, compiles it without error (icpc (ICC)
    > 10.1 20070913).
    >
    > Which compiler is right? Is it a g++ bug?


    I don't think so. Intel is often too forgiving.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 21, 2008
    #1
    1. Advertising

  2. Victor Bazarov wrote:

    > typename std::list<Closure<void, Targ> *>::const_iterator i =


    Thanks...

    > ... ; ++i)


    Any reason for this one?
     
    Matthias Buelow, Feb 21, 2008
    #2
    1. Advertising

  3. Matthias Buelow wrote:
    > Victor Bazarov wrote:
    >
    >> typename std::list<Closure<void, Targ> *>::const_iterator i =

    >
    > Thanks...
    >
    >> ... ; ++i)

    >
    > Any reason for this one?


    To return the previous iterator value i++ creates a temporary.
    Since you don't care to use the previous value, and only need
    to increment the iterator, ++i does what you need without the
    unneeded temporary.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 21, 2008
    #3
  4. Victor Bazarov wrote:

    > To return the previous iterator value i++ creates a temporary.
    > Since you don't care to use the previous value, and only need
    > to increment the iterator, ++i does what you need without the
    > unneeded temporary.


    Ah, yes.. I see.
    Probably irrelevant in this case but maybe a good thing to adopt in
    general when using iterators.
     
    Matthias Buelow, Feb 21, 2008
    #4
    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. Owen Jacobson

    Template oddness with libpqxx

    Owen Jacobson, Aug 2, 2003, in forum: C++
    Replies:
    3
    Views:
    520
    Shane Neph
    Aug 3, 2003
  2. Marc Schellens

    template specification

    Marc Schellens, Oct 2, 2003, in forum: C++
    Replies:
    4
    Views:
    357
    Marc Schellens
    Oct 4, 2003
  3. Marek Vondrak
    Replies:
    9
    Views:
    332
    Marek Vondrak
    May 15, 2006
  4. Matthias Buelow

    template specification oddness

    Matthias Buelow, Feb 21, 2008, in forum: C++
    Replies:
    4
    Views:
    340
    James Kanze
    Feb 23, 2008
  5. Replies:
    10
    Views:
    513
    Eric Pruneau
    Jun 20, 2008
Loading...

Share This Page