Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template arguments

Reply
Thread Tools

Template arguments

 
 
Buster Copley
Guest
Posts: n/a
 
      08-04-2003
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-04-2003
On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Buster Copley
Guest
Posts: n/a
 
      08-04-2003
Alf P. Steinbach wrote:
> On Mon, 04 Aug 2003 05:26:55 +0100, Buster Copley <(E-Mail Removed)> 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

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      08-04-2003
Buster Copley wrote in news:bgknb3$iue$(E-Mail Removed):

> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments and default parameters avasilev C++ 2 10-06-2011 01:40 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM
problem calling method from parent of an object if object relayson template arguments of current template class? =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?= C++ 2 03-02-2006 05:45 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments