test[0u] of type bool[1u] does not have a boolean value
monarch_dodra
monarchdodra at gmail.com
Sat Jun 29 05:50:53 PDT 2013
On Saturday, 29 June 2013 at 12:41:12 UTC, Namespace wrote:
> Is this a bug or is it just me? It seems that the compiler
> dereference wrong.
> ----
> import std.stdio;
>
> void foo(bool[1]* test) {
> if (test[0])
> test[0] = false;
> }
>
> void main()
> {
> bool[1] test = false;
> foo(&test);
> }
> ----
> prints: Error: expression test[0u] of type bool[1u] does not
> have a boolean value
>
> This work:
> ----
> if ((*test)[0])
> test[0] = false;
> ----
bool[1]*: a pointer to a static array of bools of size 1.
Ergo test[0] is of type "bool[1]". Which can't be evaluated to
bool.
When you write "test[0] = false", that is actually an *array
assignement* (test[0] is the same as *test, which resolves to a
bool[1]), and yo are assigning false to *all* (in this case, 1)
elements of your array.
On the other hand, (*test)[0] first dereferences the pointer to
obtain the array, and then obtains the first element... The
assignment on the next line is still wrong though.
So I think it's just you ;)
But in your defense, (I think you have a C++ background?) the
declaration syntax from D to C++ is completely different...
Related: I think this might actually give you a compiler warning
about doing a range assign without slicing? Bearophile had
suggested this shouldn't work unless you actually type:
"test[0][] = false;"
But I prefer:
"test[0] []= false;"
I can't test right now: Does your code emit no warnings with -w ?
More information about the Digitalmars-d-learn
mailing list