473,748 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implicit conversion from T (&)[N] to ptrcarray<T>

Here is a question about implicit conversion from T (&)[N] to ptrcarray<T>.

I wrote a class

template <class T>
struct ptrcarray
{
T * array;
size_t size;

ptrcarray() : array(NULL), size(0) { }
template <size_t N> ptrcarray(T (&array)[N]) : array(array), size(N) { }
};
Given a function

void f(const ptrcarray<int>& p);

then I expect the following code to compile

int a[] = { 1, 2, 3 };
f(a);

I expect the call to f(a) to convert 'a' of type int(&)[3] to a
ptrcarray<int> because of the implicit constructor ptrcarray<T>::p trcarray(T
(&)[N]) with T as int and N as 3.

But both Borland and g++ give an error.

e.cc: In function `int realmain(int, const char *const *, const char *const
*)':

Borland 6.0 error ==>
e.cc:33: conversion from `int *' to non-scalar type `ptrcarray<int> '
requested
e.cc:20: in passing argument 1 of `f(const ptrcarray<int> &)'

g++ 2.95.2-6 error ==>
e.cc:33: conversion from `int *' to non-scalar type `ptrcarray<int> '
requested
e.cc:20: in passing argument 1 of `f(const ptrcarray<int> &)'
But if I specify the type explicitly, then it compiles and runs fine on g++
(but on Borland I get an internal compiler error).

int a[] = { 1, 2, 3 };
f(ptrcarray<int >(a));
The question: Are my compilers broken or is my code broken (probably the
latter, but I need to check)?

The full program follows for anyone interested:
//////////////////////////////////////////////////
// realmain.cpp

#include <string>
#include <iostream>

using std::cout;

template <class T>
struct ptrcarray
{
T * array;
size_t size;

ptrcarray() : array(NULL), size(0) { }
template <size_t N> ptrcarray(T (&array)[N]) : array(array), size(N) { }
};

void f(const ptrcarray<int>& p)
{
cout << static_cast<voi d*>(p.array) << '\t' << p.size << '\n';
}

void g(const ptrcarray<const int>& p)
{
cout << static_cast<con st void*>(p.array) << '\t' << p.size << '\n';
}

int realmain(int argc, char const *const * argv, char const *const * env)
{
int a[] = { 1, 2, 3 };
const int b[] = { 1, 2, 3 };
//f(a);
//g(b);
f(ptrcarray<int >(a));
g(ptrcarray<con st int>(b));
return 0;
}

//////////////////////////////////////////////////
// main.cpp

#include <exception>

int main(int argc, char * * argv, char * * env)
{
using std::cerr;
try
{
return realmain(
argc,
const_cast<cons t char *const *>(argv),
const_cast<cons t char *const *>(env)
);
}
catch (const std::exception& e)
{
cerr << "exception: " << e.what() << "\n";
}
catch (...)
{
cerr << "unhandled exception\n";
}
return 1;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #1
3 2076
g++ has some fine, but non-standard, extensions.
Try this. It was compiled using VS6, but should compile with all C++ compilers:

#include <iostream>
using std::cout;

#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

template<typena me T> class ptrcarray
{
public:
T* array;
size_t size;

ptrcarray(const T* pArr = 0, size_t N = 0) : array(0), size(N)
{ if (!pArr)
return;
array = new T[size];
memcpy(array, pArr, size * sizeof(*array)) ;
}
~ptrcarray()
{ delete array; }
};

void f(const ptrcarray<int>& p)
{
cout << p.array << '\t' << p.size << '\n';
}

int main(int argc, char* argv[])
{
int a[] = { 1, 2, 3 };
const int b[] = { 1, 2, 3 };

ptrcarray<int> ca1(a, NELEMS(a));
f(ca1);

const ptrcarray<int> ca2(a, NELEMS(a));
f(ca2);

ptrcarray<int> cb1(b, NELEMS(b));
f(cb1);

const ptrcarray<int> cb2(b, NELEMS(b));
f(cb2);

return 0;
}

Jul 23 '05 #2

Siemel Naran wrote:
Here is a question about implicit conversion from T (&)[N] to ptrcarray<T>.
I wrote a class

template <class T>
struct ptrcarray
{
template <size_t N> ptrcarray(T (&array)[N]) { }
}; Given a function

void f(const ptrcarray<int>& p);

then I expect the following code to compile

int a[] = { 1, 2, 3 };
f(a);

I expect the call to f(a) to convert 'a' of type int(&)[3] to a
ptrcarray<int> because of the implicit constructor ptrcarray<T>::p trcarray(T (&)[N]) with T as int and N as 3.


Won't work. The N cannot be deduced in this context. Basically, you're
trying to mimic template function argument deduction. However, this is
not a function call context but a conversion context. You're looking
for a ctor in ptrcarray<int> that takes an int[3]. That's not
possible, see 14.8.2.1 to 14.8.2.3

Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #3
msalters wrote in news:1112952308 .440758.151330
@f14g2000cwb.go oglegroups.com in comp.lang.c++:
I expect the call to f(a) to convert 'a' of type int(&)[3] to a
ptrcarray<int> because of the implicit constructor ptrcarray<T>::p trcarray(T
(&)[N]) with T as int and N as 3.


Won't work. The N cannot be deduced in this context. Basically, you're
trying to mimic template function argument deduction. However, this is
not a function call context but a conversion context.


The conversion requires matching a ctor, which requires overload
resolution which (in this case) requires template argument
deduction.
You're looking
for a ctor in ptrcarray<int> that takes an int[3]. That's not
possible, see 14.8.2.1 to 14.8.2.3


#include <ostream>
#include <iostream>
#include <cstdlib>

template <class T>
struct ptrcarray
{
T * array;
size_t size;

ptrcarray() : array(NULL), size(0) { }
template <size_t N> ptrcarray(T (&array)[N]) : array(array), size(N)
{ }
};
void f(const ptrcarray<int>& p) {}
int main()
{
int a[] = { 1, 2, 3 };
f(a);
std::cout << "OK\n";
}

Compiles fine for me, gcc 3.4, bcc 6.0 (preview not builder) and
MSVC 7.1. gcc 3.2 (the ealiest gcc I have access too) failes with
some spurious `const` conversion problem. I didn't try with bcc 5.x,
but from past experience I know this compiler *always* decays array's
to pointers when making function calls, so its never able to deduce N.

To the OP, upgrade your compiler: http://www.mingw.org/

Alas there isn't (AFAICT) an upgrade option for C++ builder (yet)
but they did spam me with a questionare about what what I would
want from ther next version (*) so maybe it will appear at some point.

*) ISO C++ conformance (duh).

Rob.
--
http://www.victim-prime.dsl.pipex.com/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6447
by: Beznas | last post by:
Hi All; I'm trying to create an ASP function called CleanX that removes the punctuation and some characters like (*&^%$#@!<>?"}|{..) from a text string I came up with this but It doesn't look like it's working. Can anyone help please. THANK YOU.
2
10567
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
12
9472
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA element. That did not parse. Here is my question: How can I get attribute values to not get converted from &apos; to '
2
22954
by: Francesco Moi | last post by:
Hello. I designed a form to edit some DataBase's fields. But some of these fields contain '&lt;' and '&gt;' characters. And these characters are '<' and '>' in HTML. So if want to edit these fields, the form converts them into '<' and '>'. And I want to mantain '&lt;' and '&gt;' Mi piece of code:
11
5150
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
1
5457
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by XmltextReader.ReadString(), I get an exception when I have
2
10173
by: Jasonkimberson | last post by:
I am doing a data pull of HTML from a database that will be put into a drop down menu currently after i pull and populate the information, it converts my < into &lt; whats the work around for this? HTML code
1
2711
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by XmltextReader.ReadString(), I get an exception when I have
4
11950
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report> </myXml> I there a way that the XSLT transformation can render the content as html rather than text?
0
9558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9253
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.