Arguments for templates with value parameters

Discussion in 'C++' started by DaKoadMunky, Sep 21, 2004.

  1. DaKoadMunky

    DaKoadMunky Guest

    <CODE>

    template<int N>
    int ReturnN()
    {
    return N;
    }

    int anInt = 32;

    int main()
    {
    ReturnN<16>();

    ReturnN<anInt>();

    return 0;
    }

    </CODE>

    The code above compiles using VC++.NET 7.00.9466.

    It fails to compile using the online compiler at
    www.comeaucomputing.com/tryitout (default settings with target of Windows XP)
    with the following message...

    <ERROR>

    "ComeauTest.c", line 13: error: expression must have a constant value
    ReturnN<anInt>();
    ^

    1 error detected in the compilation of "ComeauTest.c".

    </ERROR>

    Can anyone tell me which compiler is correct?

    I assume it is Comeau, but you never know.

    FYI...The VC++.NET program does produce correct results. The version of the
    function invoked by "ReturnN<anInt>()" instead of returning a hardcoded value
    actually returns the result of dereferencing a hardcoded address (in this case
    the address of the global variable anInt.)

    Thanks.
    DaKoadMunky, Sep 21, 2004
    #1
    1. Advertising

  2. DaKoadMunky

    Sharad Kala Guest

    "DaKoadMunky" <> wrote in message
    > <CODE>
    >
    > template<int N>
    > int ReturnN()
    > {
    > return N;
    > }
    >
    > int anInt = 32;
    >
    > int main()
    > {
    > ReturnN<16>();
    >
    > ReturnN<anInt>();
    >
    > return 0;
    > }
    >
    > </CODE>
    >
    > The code above compiles using VC++.NET 7.00.9466.
    >
    > It fails to compile using the online compiler at
    > www.comeaucomputing.com/tryitout (default settings with target of Windows

    XP)
    > with the following message...
    >
    > Can anyone tell me which compiler is correct?


    Comeau, reason being that template arguments must be known at compile time.
    Comeau will accept the code if you make anInt const or a macro.

    Sharad
    Sharad Kala, Sep 21, 2004
    #2
    1. Advertising

  3. DaKoadMunky wrote in news: in
    comp.lang.c++:

    > Can anyone tell me which compiler is correct?
    >
    > I assume it is Comeau, but you never know.
    >
    >


    Its Comeau, non-type template arguments must be compile-time
    integral constants, or constant pointers or references.

    template < int N > int f() { return N; }

    enum Named_enum { value = 1 };
    int const another = 2;

    struct aStruct
    {
    static int const value = 3;
    };

    struct anotherStruct
    {
    enum Named_enum { value = 4 };
    };

    char array[5];

    int main()
    {
    f< value >();
    f< another >();
    f< aStruct::value >();
    f< anotherStruct::value >();

    f< sizeof( array ) >();
    }

    Note that sizeof( array ) is a std::size_t integral constant,
    but the rules allow it to be converted to an int integral const.

    I gave the enum's a name (i.e not enum { value = 1 };) as with
    some older compilers this helps, but strictly speaking it isn't
    required.

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Sep 21, 2004
    #3
  4. "Sharad Kala" <> schrieb im Newsbeitrag
    news:...
    >
    > "DaKoadMunky" <> wrote in message
    >> <CODE>
    >>
    >> template<int N>
    >> int ReturnN()
    >> {
    >> return N;
    >> }
    >>
    >> int anInt = 32;
    >>
    >> int main()
    >> {
    >> ReturnN<16>();
    >>
    >> ReturnN<anInt>();
    >>
    >> return 0;
    >> }
    >>
    >> </CODE>
    >>
    >> The code above compiles using VC++.NET 7.00.9466.
    >>
    >> It fails to compile using the online compiler at
    >> www.comeaucomputing.com/tryitout (default settings with target of
    >> Windows

    > XP)
    >> with the following message...
    >>
    >> Can anyone tell me which compiler is correct?

    >
    > Comeau, reason being that template arguments must be known at
    > compile time.
    > Comeau will accept the code if you make anInt const or a macro.
    >
    > Sharad



    So, does VC then use 'typeof anInt' or simply use an 'int' if it
    doesn't know what to use? (The way VC 6 did things, I think)
    Gernot Frisch, Sep 21, 2004
    #4
  5. DaKoadMunky

    JKop Guest

    According to the Standard, the program is illformed.

    But in practicality, the aformentioned compiler *can*
    figure out what's going on and it proceeds in such a
    fashion.

    I'd say there all sorts of compiler switches for the
    aforementioned compiler that will actually compiler your
    code successfully but then say:

    Note: Standard C++ forbids non-const template parameter.

    -JKop
    JKop, Sep 21, 2004
    #5
  6. DaKoadMunky

    Sharad Kala Guest

    "Gernot Frisch" <> wrote in message

    > So, does VC then use 'typeof anInt' or simply use an 'int' if it
    > doesn't know what to use? (The way VC 6 did things, I think)


    I have no idea as to how VC is compiling it. Also note that VC 6 has quite
    poor template support.

    Sharad
    Sharad Kala, Sep 21, 2004
    #6
  7. DaKoadMunky

    Greg Comeau Guest

    In article <>,
    DaKoadMunky <> wrote:
    ><CODE>
    >
    >template<int N>
    >int ReturnN()
    >{
    > return N;
    >}
    >
    >int anInt = 32;
    >
    >int main()
    >{
    > ReturnN<16>();
    >
    > ReturnN<anInt>();
    >
    > return 0;
    >}
    >
    ></CODE>
    >
    >The code above compiles using VC++.NET 7.00.9466.
    >
    >It fails to compile using the online compiler at
    >www.comeaucomputing.com/tryitout (default settings with target of Windows XP)
    >with the following message...
    >
    ><ERROR>
    >
    >"ComeauTest.c", line 13: error: expression must have a constant value
    > ReturnN<anInt>();
    > ^
    >
    >1 error detected in the compilation of "ComeauTest.c".
    >
    ></ERROR>
    >
    >Can anyone tell me which compiler is correct?
    >
    >I assume it is Comeau, but you never know.
    >
    >FYI...The VC++.NET program does produce correct results. The version of the
    >function invoked by "ReturnN<anInt>()" instead of returning a hardcoded value
    >actually returns the result of dereferencing a hardcoded address (in this case
    >the address of the global variable anInt.)


    In addition to Comeau C++, VC++ 6.0 and VC++ 7.1 also give errors
    for line 13, as they should, so this must be a VC++ 7.0ism.

    BTW, Comeau online does not have a target of Windows XP,
    it's target is a faux platform.
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Sep 21, 2004
    #7
  8. DaKoadMunky

    Greg Comeau Guest

    In article <>,
    Sharad Kala <> wrote:
    >
    >"DaKoadMunky" <> wrote in message
    >> <CODE>
    >>
    >> template<int N>
    >> int ReturnN()
    >> {
    >> return N;
    >> }
    >>
    >> int anInt = 32;
    >>
    >> int main()
    >> {
    >> ReturnN<16>();
    >>
    >> ReturnN<anInt>();
    >>
    >> return 0;
    >> }
    >>
    >> </CODE>
    >>
    >> The code above compiles using VC++.NET 7.00.9466.
    >>
    >> It fails to compile using the online compiler at
    >> www.comeaucomputing.com/tryitout (default settings with target of Windows

    >XP)
    >> with the following message...
    >>
    >> Can anyone tell me which compiler is correct?

    >
    >Comeau, reason being that template arguments must be known at compile time.
    >Comeau will accept the code if you make anInt const or a macro.


    It's not just that the arg must be known, or be const,
    it's that when using something like 'int N' then it's
    preferring an int constant as the argument.
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Sep 21, 2004
    #8
  9. DaKoadMunky

    Sharad Kala Guest

    "Greg Comeau" <> wrote in message

    > It's not just that the arg must be known, or be const,
    > it's that when using something like 'int N' then it's
    > preferring an int constant as the argument.


    Could you elaborate as to what you mean by 'preferring' in this context ?

    Thanks,
    Sharad
    Sharad Kala, Sep 22, 2004
    #9
  10. DaKoadMunky

    Greg Comeau Guest

    In article <>,
    Sharad Kala <> wrote:
    >"Greg Comeau" <> wrote in message
    >> It's not just that the arg must be known, or be const,
    >> it's that when using something like 'int N' then it's
    >> preferring an int constant as the argument.

    >
    >Could you elaborate as to what you mean by 'preferring' in this context ?



    C:\Comeau C++>type intn.cpp
    template <int N>
    struct xyz {};

    int i;
    const int ci = 99;

    enum { blah };

    int main()
    {
    // these are "preferred", as in allowed:
    xyz<999> x999;
    xyz<ci> x99;
    xyz<'c'> xc;
    xyz<blah> xblah;

    // these are errors:
    xyz<99.99> x99d99;
    xyz<i> xi;

    return 0;
    }

    C:\Comeau C++>como --A intn.cpp
    Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
    Copyright 1988-2004 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "intn.cpp", line 18: error: expression must have integral or enum type
    xyz<99.99> x99d99;
    ^

    "intn.cpp", line 19: error: expression must have a constant value
    xyz<i> xi;
    ^

    .....

    So i is known, and 99.99 is a constant, but that alone is insufficient.
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Sep 22, 2004
    #10
    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. Fred
    Replies:
    1
    Views:
    594
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    379
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    467
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    364
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    799
    recover
    Jul 25, 2006
Loading...

Share This Page