473,387 Members | 1,440 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,387 software developers and data experts.

c++ type system

template
<
typename T,
unsigned int size
>
void DoStuff1(
T (& array)[size]
)
{}

template
<
typename T,
unsigned int size
>
void DoStuff2(
T (* array_p)[size]
)
{}

struct Pod
{
int data[10];
};

int main ()
{
Pod pod;
DoStuff1(pod.data); // does not compile why ??
DoStuff2(&pod.data); // this one is fine in comparison to the
above
}

I am just confused as how come a function parameter taking a reference
to an array leads to the data array decaying into a pointer (so type
info is lost and code does not compile), where as the function which
is taking a call parameter of a pointer to an array can compile and
keep on holding the type info.
Jul 10 '08 #1
4 1488
On Jul 10, 11:01*am, Looney <hardy_melbou...@hotmail.comwrote:
template
<
* * * * typename T,
* * * * unsigned int size

void DoStuff1(
* * * * T (& array)[size]
)
{}

template
<
* * * * typename T,
* * * * unsigned int size

void DoStuff2(
* * * * T (* array_p)[size]
)
{}

struct Pod
{
* * * * int data[10];

};

int main ()
{
* * Pod pod;
* * DoStuff1(pod.data); // does not compile why ??
* * DoStuff2(&pod.data); // this one is fine in comparison to the
above

}

I am just confused as how come a function parameter taking a reference
to an array leads to the data array decaying into a pointer (so type
info is lost and code does not compile), where as the function which
is taking a call parameter of a pointer to an array can compile and
keep on holding the type info.
I do suspect it is due the implicit conversion applied to the array
object to make the reference to the array.
is n't it ?
Jul 10 '08 #2
//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop

//---------------------------------------------------------------------------

template
<
typename T,
unsigned int size
>
void DoStuff1(
T (& array)[size]
)
{}

template
<
typename T,
unsigned int size
>
void DoStuff2(
T (* array_p)[size]
)
{}

struct Pod
{
int data[10];
};
#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
Pod pod;

int (& arrayref)[10] = pod.data;
DoStuff1(arrayref); // fine it compiles

DoStuff1(pod.data); // does not compile

DoStuff2(&pod.data);// fine it compiles

return 0;
}
//---------------------------------------------------------------------------

Jul 10 '08 #3
Looney wrote:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
[...]

I removed the Borland-specific stuff and shortened the example to:

//---------------------
template <typename T, unsigned int size>
void DoStuff1( T (& array)[size] )
{}

struct Pod
{
int data[10];
};

int main()
{
Pod pod;
DoStuff1(pod.data); // does not compile
return 0;
}
//---------------------

This doesn't compile with the Borland compiler, the error message is:

E2285 Could not find a match for 'DoStuff1<T,size>(int *)'

However, it compiles with GCC and Comeau, and using the array directly
without the struct it compiles with the Borland compiler, too:

int data[10];
DoStuff1(data); // compiles.

I guess it is a compiler error.

--
Thomas
Jul 10 '08 #4
On Jul 10, 12:00*pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
Looney wrote:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop

[...]

I removed the Borland-specific stuff and shortened the example to:

//---------------------
template <typename T, unsigned int size>
void DoStuff1( T (& array)[size] )
{}

struct Pod
{
* * * * int data[10];

};

int main()
{
* * * * Pod pod;
* * * * DoStuff1(pod.data); // does not compile
* * * * return 0;}

//---------------------

This doesn't compile with the Borland compiler, the error message is:

E2285 Could not find a match for 'DoStuff1<T,size>(int *)'

However, it compiles with GCC and Comeau, and using the array directly
without the struct it compiles with the Borland compiler, too:

int data[10];
DoStuff1(data); *// compiles.

I guess it is a compiler error.

--
Thomas
Thanks for summing all of it up much more clearly & precisely.
It does seem bit odd and does indeed look like a compiler error.
Jul 10 '08 #5

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

Similar topics

5
by: jorfei | last post by:
I have written a component with a property IPAdrress of type System.Net.IPAddress. To ease the configuration of the component at design time, I have written a type converter for the type...
3
by: Mike in Paradise | last post by:
I have an application that is being passed objects which could either be an instance or a Type in the case of a Static Class When you do the GetType on the object that was originally a Static...
1
by: mrkurt | last post by:
I've built a few webservices locally that work just fine. Deployed them to a server and I get the following exception. This exception happens on even the most basic webmethod call. Hitting the...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
0
by: qiang | last post by:
Hi everyone, Could you please take a look at an exception for ASP.NET application? My ASP.NET application is using Infragistics WebChart control. I encounter an exception below when...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.