473,324 Members | 2,370 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,324 software developers and data experts.

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>::ptrcarray(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<void*>(p.array) << '\t' << p.size << '\n';
}

void g(const ptrcarray<const int>& p)
{
cout << static_cast<const 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<const 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<const char *const *>(argv),
const_cast<const 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++.moderated. First time posters: Do this! ]
Jul 23 '05 #1
3 2057
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<typename 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>::ptrcarray(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++.moderated. First time posters: Do this! ]
Jul 23 '05 #3
msalters wrote in news:1112952308.440758.151330
@f14g2000cwb.googlegroups.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>::ptrcarray(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++.moderated. 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
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...
2
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...
12
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...
2
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...
11
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...
1
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>...
2
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...
1
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>...
4
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>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.