473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template arguments

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
3 3384
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bjam | last post by:
Hi, today I was able to abstract out into a separate xsl file a template that I specifically perform a call templates for, this worked with import no problem. However, when trying to do the same...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
9
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8,...
4
by: Lighter | last post by:
Why is template function not allowed to have defaut arguments? We know that class template is allowed to have default arguments in C++ standard, why is template function not? I can't think out...
2
by: desktop | last post by:
I have this code from the book C++ Templates: The Complete Guide: #ifndef ACCUM8_HPP_ #define ACCUM8_HPP_ #include "accumtraits4.hpp" #include "sumpolicy2.hpp"
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
3
by: nooneinparticular314159 | last post by:
I'm trying to do a template with specialization. I declare two templates, one of which looks like: template<int a, int b> struct MOO { enum{ VALUE = b == 0 ? a: MOO<some stuff>::VALUE };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.