By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Template arguments

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <bu****@none.com> 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.

Jul 19 '05 #2

P: n/a
Alf P. Steinbach wrote:
On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <bu****@none.com> 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

Jul 19 '05 #3

P: n/a
Buster Copley wrote in news:bg**********@news7.svr.pol.co.uk:
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/
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.