Template arguments

B

Buster Copley

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
 
A

Alf P. Steinbach

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.
 
B

Buster Copley

Alf said:
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 don't see any syntactic errors.

Thanks,
Buster
 
R

Rob Williscroft

Buster Copley wrote in
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top