c++ type system

L

Looney

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

template
<
typename T,
unsigned int sizevoid 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.
 
L

Looney

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 ?
 
L

Looney

* Looney:


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 ??

With which compiler and options does this not compile, and what's the error message?

With which compiler and options does this not compile, and what's the error message?
where as the function which
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 ?

With which compiler and options does this not compile, and what's the error message?

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I am using code gear borland c++ 2007
There are a lot of compiler options. i am unsure which ones woudl be
of interest in this regard... If you still insist i can provide them.

also to add to that code snippet above in the main function bizarrely
this compiles

int (& arrayref)[10] = pod.data;
DoStuff1(arrayref);
 
L

Looney

* Looney:


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 ??

With which compiler and options does this not compile, and what's the error message?

With which compiler and options does this not compile, and what's the error message?
where as the function which
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 ?

With which compiler and options does this not compile, and what's the error message?

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

sorry i forgot the error message says

[BCC32 Error] File1.cpp(43): E2285 Could not find a match for
'DoStuff1<T,size>(int *)'
Full parser context
File1.cpp(37): parsing: int __stdcall WinMain(void *,void *,char
*,int)
 
L

Looney

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

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

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

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

template
<
typename T,
unsigned int sizevoid 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;
}
//---------------------------------------------------------------------------
 
T

Thomas J. Gritzan

Looney said:
//---------------------------------------------------------------------------

#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.
 
L

Looney

Looney said:
//---------------------------------------------------------------------------
#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.

Thanks for summing all of it up much more clearly & precisely.
It does seem bit odd and does indeed look like a compiler error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top