P
Paul
Leigh Johnston said:On 21/03/2011 18:40, Paul wrote:
On 21 Mrz., 02:00, Paul wrote:
Or alternatively they could simply compile and run the following
code:
template<typename T>
void foo(T p){
std::cout<< "\nIn primary foo:\t" << typeid(T).name();
}
template<>
void foo<int*>(int* p){
std::cout<< "\nIn specialized foo:\t" << typeid(p).name();
}
int main(){
const int s = 2;
int p1[1];
int p2;
foo(p1); // #1
foo(p2); // #2
}
--I guess you intended to prove "an array is just a pointer"
again.
-- But
I never have tried to prove this, but nonetheless for all
intensive
purposes that's how the language works.
What I have been arguing is againt the idiots who suggest p is
not a
pointer to an array, with:
int* p= new int[16];
--this code example only shows that template argument deduction
picks
--T=int* respectivly T=int(*)[2]. It does not prove that the
arrays
--denoted by p1 and p2 are pointers. If you think otherwise, look
up the
--term "array-to-pointer decay" and check out the template
argument
--deduction rules in the C++ ISO standard.
--BTW: Are you aware of the following?
-- template<class T>
-- void foo(T & ref);
-- :
-- :
-- foo(p1); // --> T = int[1], T& = int(&)[1]
-- foo(p2); // --> T = int[2][2], T& = int(&)[2][2]
--In both cases T& is the type of a reference to an _array_, not a
--_pointer_.
A reference to an array of int:
int (&r)[5]
is the same type as int* p, not int(*p)[5].
Are you one of those who think p is not a pointer to an array
because a
pointer to an array must be type int(*)[SIZE] ?
If you want conclusive proof simply use a specialized traits
template:
template<typename T, typename T1>
struct is_same_type{
enum {value = 0};
};
template<typename T>
struct is_same_type<T,T>{
enum {value = 1};
};
template<typename T1, typename T2>
void foo(T1 p1, T2 p2){
if(is_same_type<T1, T2>::value) std::cout<< "Types are the
same\n" ;
else std::cout<< "Types are different\n";
}
int main(){
int arr[5]={0};
int* p1 ='\0';
int (*p2)[5] ='\0';
int (&r)[5] = arr;
foo(arr, p1); //Same type
foo(arr, p2); //Different type
foo(arr, r); //Same type
foo( r, p1); //Same type
foo( r, p2); //Different type
}
Tuition fees extra , TYVM and goodbye .
Have a nice day.
You have proven nothing:
template<typename T>
void foo(T v)
{
std::cout<< "Not an array\n";
}
v's type could be any type: an array of types, a pointer to an
array ,
or an array of function pointers.
No it couldn't given the other overload below which will be used for
arrays.
I'm afraid once again I must prove you wrong:
#include <iostream>
template<typename T>
void foo(T v)
{
std::cout<< "\nAn object of type:\t" << typeid(T).name() << " <- Not
an
array?\n" ;;
}
template <typename T, std::size_t S>
void foo(T (&a))
{
std::cout<< "\nAn array of type:\t" << typeid(T).name();
}
int main()
{
int arr[6];
int (*parr)[6] = new int[5][6];
foo(arr);
foo(parr);
}
It's about time you learned to accept defeat.
You fail yet again. A "pointer to an array" is not the same as an
"array".
It's not a pointer to an array though, The object pointed-to is an array
of pointers to arrays (a 2dim array). This is what you do not seem to
understand.
No it is not an array of pointers; there is only one pointer namely the
pointer to the array which is itself not an array as it is .. wait for
it .. a pointer. So you fail yet again; you are a lost cause.
#include <iostream>
template<typename T>
void foo(T v)
{
std::cout<< "\nAn object of type:\t" << typeid(T).name();
}
template <typename T, std::size_t S>
void foo(T (&a))
{
std::cout<< "\nAn array of type:\t" << typeid(T).name();
}
template <typename T, std::size_t S>
void foo(T (*a))
{
std::cout<< "\nA pointer to an array of type:\t" << typeid(T).name();
This is wrong, it produces the output:
"A pointer to an array of type: int" .
The object pointed to is a 2dim array of type int(*).
Your code cannot tell the difference between 1dim arrays and 2dim
arrays.
As far as the ambiguity of the template overloads is concerned this is
just a feature of the fact that arrays can decay to pointers. Just
because arrays can decay to pointers does not mean that arrays are
pointers.
SO your template system cannot define between a 1dim array and a 2diim
array? You just treat a 2dim dynamic array is if it were a 1dim array of
pointers.