Template arguments

Discussion in 'C++' started by Buster Copley, Aug 4, 2003.

  1. Compiling the following code gives rise to the error indicated.

    template <typename T, int N> void f (const T (& u) [N]) { }
    void g (const int (& u) [2]) { }

    int main ()
    {
    int x [2];
    f (x); // ERROR
    g (x);
    }

    (G++ 3.2.2): no matching function for call to `f(int[2])'
    (Comeau online): no instance of function template "f" matches the
    argument list ... The argument types that you used are: (int [2])

    I didn't boot into Windows to try Borland C++ Builder 6, but IIRC
    its compiler has very little success with references to arrays.

    There is no error for the call of g ().

    For my real problem I can use either a small set of overloaded
    functions, or a partially ordered pair of function templates.
    I'd rather use the latter, but only because it's cool. I don't
    know whether I'm making a simple syntactic error, or merely
    attempting the impossible. Any ideas?

    Regards,
    Buster
    Buster Copley, Aug 4, 2003
    #1
    1. Advertising

  2. On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <> wrote:

    >Compiling the following code gives rise to the error indicated.
    >
    >template <typename T, int N> void f (const T (& u) [N]) { }
    >void g (const int (& u) [2]) { }
    >
    >int main ()
    >{
    > int x [2];
    > f (x); // ERROR
    > g (x);
    >}
    >
    >(G++ 3.2.2): no matching function for call to `f(int[2])'
    >(Comeau online): no instance of function template "f" matches the
    >argument list ... The argument types that you used are: (int [2])


    Compiles OK with VC 7.0.



    >I didn't boot into Windows to try Borland C++ Builder 6, but IIRC
    >its compiler has very little success with references to arrays.
    >
    >There is no error for the call of g ().
    >
    >For my real problem I can use either a small set of overloaded
    >functions, or a partially ordered pair of function templates.
    >I'd rather use the latter, but only because it's cool. I don't
    >know whether I'm making a simple syntactic error, or merely
    >attempting the impossible. Any ideas?


    I don't see any syntactic errors.
    Alf P. Steinbach, Aug 4, 2003
    #2
    1. Advertising

  3. Alf P. Steinbach wrote:
    > On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <> wrote:
    >
    >>Compiling the following code gives rise to the error indicated.
    >>
    >>template <typename T, int N> void f (const T (& u) [N]) { }
    >>void g (const int (& u) [2]) { }
    >>
    >>int main ()
    >>{
    >> int x [2];
    >> f (x); // ERROR
    >> g (x);
    >>}
    >>
    >>(G++ 3.2.2): no matching function for call to `f(int[2])'
    >>(Comeau online): no instance of function template "f" matches the
    >>argument list ... The argument types that you used are: (int [2])

    >
    > Compiles OK with VC 7.0.


    Frankly, I'm shocked! I would not have expected VC to accept
    standard-compliant code (if such it is) rejected by Comeau.
    Thank you for checking this out.

    >>I didn't boot into Windows to try Borland C++ Builder 6, but IIRC
    >>its compiler has very little success with references to arrays.
    >>
    >>There is no error for the call of g ().
    >>
    >>For my real problem I can use either a small set of overloaded
    >>functions, or a partially ordered pair of function templates.
    >>I'd rather use the latter, but only because it's cool. I don't
    >>know whether I'm making a simple syntactic error, or merely
    >>attempting the impossible. Any ideas?

    >
    > I don't see any syntactic errors.


    Thanks,
    Buster
    Buster Copley, Aug 4, 2003
    #3
  4. Buster Copley wrote in news:bgknb3$iue$:

    > Compiling the following code gives rise to the error indicated.
    >
    > template <typename T, int N> void f (const T (& u) [N]) { }
    > void g (const int (& u) [2]) { }
    >
    > int main ()
    > {
    > int x [2];
    > f (x); // ERROR
    > g (x);
    > }
    >
    > (G++ 3.2.2): no matching function for call to `f(int[2])'
    > (Comeau online): no instance of function template "f" matches the
    > argument list ... The argument types that you used are: (int [2])
    >


    It compiles ok if you remove the const from the template though, i.e
    template <typename T, int N> void f (T (& u) [N]) { }

    Also note it compiles:

    #include <iostream>
    #include <ostream>

    template <typename T, int N> void f (T const (&) [N])
    {
    std::cerr << "const\n";
    }
    template <typename T, int N> void f (T (&) [N])
    {
    std::cerr << "mutable\n";
    }

    int main ()
    {
    int x [2];

    f( x );

    #if defined(__BORLANDC__)

    char const cca[] = "moose";
    f( cca );

    #else

    f( "moose" );

    #endif
    }

    This compiles with g++ (3.2/MingW), MSVC 7.1 and bcc32 (5.5.1)
    and the output is:
    mutable
    const

    I've no idea what the Comeau/EDG outputs.

    Note the __BORLANDC__ workaround is the because otherwise it tries
    to instantiate f( char * ), how broken is that!

    > I didn't boot into Windows to try Borland C++ Builder 6, but IIRC
    > its compiler has very little success with references to arrays.
    >


    My bcc32 compiles your original code, but I believe its because
    it simply ignores const in templates, FWIW MSVC 7.1 compiles it too.

    > There is no error for the call of g ().
    >
    > For my real problem I can use either a small set of overloaded
    > functions, or a partially ordered pair of function templates.
    > I'd rather use the latter, but only because it's cool. I don't
    > know whether I'm making a simple syntactic error, or merely
    > attempting the impossible. Any ideas?
    >


    The fire and forget solution (*) is:

    template <typename T, int N>
    void f_const (T const (& u) [N])
    {
    /* your real code goes here */
    }

    template <typename T, int N>
    inline void f (T const (& u) [N])
    {
    f_const( u );
    }

    template <typename T, int N>
    inline void f (T (& u) [N])
    {
    f_const( const_cast< T const (&)[N] >( u ) );
    }


    (*) I beleive this is a workaround, but a standard conforming one :).

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Aug 4, 2003
    #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. Edward Diener
    Replies:
    14
    Views:
    4,930
    Josiah Carlson
    Apr 6, 2004
  2. IR
    Replies:
    3
    Views:
    418
  3. Replies:
    1
    Views:
    2,100
    Gianni Mariani
    Jun 8, 2007
  4. avasilev
    Replies:
    2
    Views:
    510
    avasilev
    Oct 6, 2011
  5. Alex
    Replies:
    3
    Views:
    83
Loading...

Share This Page