472,780 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 2032
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.