template function in inner class

Discussion in 'C++' started by Christof Warlich, Oct 7, 2008.

  1. Hi,

    could anyone tell what's wrong with this?:

    template<int x>
    struct Outer {
    struct Inner {
    template<int y> void finner() {}
    };
    Inner inner;
    void fouter() {inner.finner<0>();}
    };

    Thanks,

    Christof
    Christof Warlich, Oct 7, 2008
    #1
    1. Advertising

  2. Christof Warlich

    Lionel B Guest

    On Tue, 07 Oct 2008 10:41:59 +0200, Christof Warlich wrote:

    > Hi,
    >
    > could anyone tell what's wrong with this?:
    >
    > template<int x>
    > struct Outer {
    > struct Inner {
    > template<int y> void finner() {}
    > };
    > Inner inner;
    > void fouter() {inner.finner<0>();}


    void fouter() {inner.template finner<0>();}

    > };


    compiles for me. I'm not clear on why this might be required.

    --
    Lionel B
    Lionel B, Oct 7, 2008
    #2
    1. Advertising

  3. Lionel B schrieb:
    > compiles for me. I'm not clear on why this might be required.


    It doesn't compile with my gcc:

    $ g++ --version
    g++ (GCC) 3.4.6
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    $ g++ -c tst.cc
    tst.cc: In member function `void Outer<x>::fouter()':
    tst.cc:8: error: expected primary-expression before ')' token

    So this may be a compiler bug?! I'll try with a more recent gcc version
    tonight. Which compiler did you use?

    Thanks and regards,

    Christof
    Christof Warlich, Oct 7, 2008
    #3
  4. Christof Warlich

    Lionel B Guest

    On Tue, 07 Oct 2008 12:38:43 +0200, Christof Warlich wrote:

    > Lionel B schrieb:
    >> compiles for me. I'm not clear on why this might be required.

    >
    > It doesn't compile with my gcc:
    >
    > $ g++ --version
    > g++ (GCC) 3.4.6
    > Copyright (C) 2006 Free Software Foundation, Inc. This is free software;
    > see the source for copying conditions. There is NO warranty; not even
    > for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ g++ -c tst.cc
    > tst.cc: In member function `void Outer<x>::fouter()': tst.cc:8: error:
    > expected primary-expression before ')' token
    >
    > So this may be a compiler bug?! I'll try with a more recent gcc version
    > tonight. Which compiler did you use?


    Odd. The amended code compiles for me here with gcc 3.4.6 (also gcc
    4.3.1, 4.3.0 4.1.2 and Intel icc 10.0).

    All the above gcc versions reject your original code, although icc
    accepts it.

    Comeau (about as standards-compliant as it gets) rejects your original
    code with:

    "ComeauTest.c", line 7: error: expected an expression
    void fouter() {inner.finner<0>();}

    and accepts the amended version.

    --
    Lionel B
    Lionel B, Oct 7, 2008
    #4
  5. Christof Warlich

    Triple-DES Guest

    On 7 Okt, 12:38, Christof Warlich <> wrote:
    > Lionel B schrieb:
    >
    > > compiles for me. I'm not clear on why this might be required.

    >
    > It doesn't compile with my gcc:
    >
    > $ g++ --version
    > g++ (GCC) 3.4.6
    > Copyright (C) 2006 Free Software Foundation, Inc.
    > This is free software; see the source for copying conditions.  There is NO
    > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    > $ g++ -c tst.cc
    > tst.cc: In member function `void Outer<x>::fouter()':
    > tst.cc:8: error: expected primary-expression before ')' token
    >
    > So this may be a compiler bug?! I'll try with a more recent gcc version
    > tonight. Which compiler did you use?
    >
    > Thanks and regards,
    >
    > Christof


    Did you try:

    template<int x>
    struct Outer {
    struct Inner {
    template<int y> void finner() {}
    };
    Inner inner;
    void fouter() {inner.template finner<0>();}
    };

    ?
    Triple-DES, Oct 7, 2008
    #5
  6. Christof Warlich

    James Kanze Guest

    On Oct 7, 10:41 am, Christof Warlich <>
    wrote:

    > could anyone tell what's wrong with this?:


    > template<int x>
    > struct Outer {
    > struct Inner {
    > template<int y> void finner() {}
    > };
    > Inner inner;
    > void fouter() {inner.finner<0>();}


    The < here is a less than operator. I doubt that that's what
    you wanted. You need to tell the compiler that inner.finner is
    a template, e.g.:

    inner. template finner< 0 >() ;

    (In earlier compilers, this might work, because they don't
    actually parse the template until instantiation. The standard
    is designed, however, so that they can, and without knowing the
    instantiation type, it can't know what inner.finner refers to.
    In order to parse correctly, however, the compiler must know
    which symbols name types, and which name templates. So you
    occasionally have to insert typename or template to keep it
    happy.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Oct 7, 2008
    #6
  7. Triple-DES schrieb:
    > Did you try:
    >
    > template<int x>
    > struct Outer {
    > struct Inner {
    > template<int y> void finner() {}
    > };
    > Inner inner;
    > void fouter() {inner.template finner<0>();}
    > };

    No, I missed Lionel's change. With the change, it works
    for me as well :).
    Christof Warlich, Oct 8, 2008
    #7
  8. James Kanze schrieb:
    > The < here is a less than operator. I doubt that that's what
    > you wanted. You need to tell the compiler that inner.finner is
    > a template, e.g.:
    >
    > inner. template finner< 0 >() ;
    >
    > (In earlier compilers, this might work, because they don't
    > actually parse the template until instantiation. The standard
    > is designed, however, so that they can, and without knowing the
    > instantiation type, it can't know what inner.finner refers to.
    > In order to parse correctly, however, the compiler must know
    > which symbols name types, and which name templates. So you
    > occasionally have to insert typename or template to keep it
    > happy.)


    .... and thanks for the background information on why the template keyword
    is needed here.
    Christof Warlich, Oct 8, 2008
    #8
    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. E11
    Replies:
    1
    Views:
    4,759
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    751
    Pete Becker
    May 4, 2005
  3. Pyenos
    Replies:
    2
    Views:
    387
    Pyenos
    Dec 27, 2006
  4. Fei Liu
    Replies:
    4
    Views:
    356
    Fei Liu
    Jan 8, 2008
  5. Ian Collins
    Replies:
    0
    Views:
    394
    Ian Collins
    Sep 18, 2008
Loading...

Share This Page