Re: Multi-dimensional Arrays
mshetty@mail.com wrote:
[...]
Is a 3D Char array equivalent to a 2D String array?
That depends :-)
Having the following code:
// 2d-3dchar.cpp
typedef char Char;
typedef char* String;
typedef Char*** The3DCharArray;
typedef String** The2DStringArray;
int
accept3DCharArray(The3DCharArray)
{
return 0;
}
int
accept2DStringArray(The2DStringArray)
{
return 1;
}
The3DCharArray a3DCharArray;
int test1 = accept2DStringArray(a3DCharArray);
The2DStringArray a2DStringArray;
int test2 = accept3DCharArray(a2DStringArray);
and compled this way:
$ g++ -c 2d-3dchar.cpp -Wall
$
It compiles cleanly, so 'The3DCharArray' is in some sence equivalent to
'The2DStringArray'. Given the definitions of 'Char' and 'String'
changed, this might not be the case anymore.
However, I'm curious is there a way to tell if some type 'T' is
equivalent ot some other type 'U'? Partial specialization comes to my
mind...
#include <iostream>
template< typename T, typename U >
struct equivalent
{
enum { result = 0 };
};
template< typename T >
struct equivalent< T, T >
{
enum { result = 1 };
};
int
main(void)
{
std::cout << equivalent< The3DCharArray, The2DStringArray >::result
<< std::endl;
std::cout << equivalent< The3DCharArray, int >::result
<< std::endl;
return 0;
}
Any obvious flaws here?
Alex
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]