digitalmars.D.learn - typeof([2,2]) !=? int[2]
- Zhenya (23/23) Mar 08 2013 Hi!
- bearophile (8/8) Mar 08 2013 This is an answer to just your title question.
Hi!
Explain me please,what's wrong with this code:
struct NDimensionalArray(T,alias size)
if(is(typeof(size) _ == int[n],int n) &&
n > 0)
{
static if(n > 1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
else
T m_array[size[0]];
alias m_array this;
}
void main()
{
NDimensionalArray!(int,[2,2]) array;
array[0][1] = 1;
}
It fails with message:
Error: template instance NDimensionalArray!(int, [2, 2])
NDimensionalArray!(int, [2, 2]) does not match template
declaration NDimensionalArray(T, alias size) if (is(typeof(size)
_ == int[n],int n) && n > 0)
on dmd git-head.
Mar 08 2013
This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far. Bye, bearophile
Mar 08 2013
On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far. Bye, bearophileThank you very much.
Mar 08 2013
On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote:Although it's a bit strange,since we can always pass heap allocated array by using new.This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like s[2,2] but nothing has happened on this so far. Bye, bearophileThank you very much.
Mar 08 2013
On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:
Your constraint could be:
if(is(typeof(size) _ == int[]) && size.length > 0)
Also it looks like you are passing 1 too many template args?
static if(n > 1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
^^
Mar 08 2013
On Friday, 8 March 2013 at 23:09:07 UTC, cal wrote:
On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote:
Your constraint could be:
if(is(typeof(size) _ == int[]) && size.length > 0)
Also it looks like you are passing 1 too many template args?
static if(n > 1)
NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];
^^
Yes,it's a typo.But it seems to be ugly pass dynamically
allocated array through
a template parameter,because it's size should be compile-time
constant.
Mar 08 2013
On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote:Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through a template parameter,because it's size should be compile-time constant.Its size is a compile-time constant because it is an array literal. The size is known inside the constraint.
Mar 08 2013
On Friday, 8 March 2013 at 23:18:52 UTC, cal wrote:On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote:Yes,really. I thought that dynamic allocation is runtime operationYes,it's a typo.But it seems to be ugly pass dynamically allocated array through a template parameter,because it's size should be compile-time constant.Its size is a compile-time constant because it is an array literal. The size is known inside the constraint.
Mar 08 2013









"Zhenya" <zheny list.ru> 