/ / メモ
2010-02-23

宣言が構造体の変数だとどう書く?

import std.stdio;

struct S
{
    int value;
}

void main()
{
    if(auto s = S(10)) //Error: expression S(10) of type S does not have a boolean value
    {
        writeln(s.value);
    }
}

こういう書き方ではないようだ。

クラスの場合はどうだろう。

import std.stdio;

class C
{
    int value;
    this(int value)
    {
        this.value = value;
    }
}

void main()
{
    if(auto c = new C(10))
    {
        writeln(c.value);
    }
}

動いた。

構造体を new してみる。

import std.stdio;

struct S
{
    int value;
    this(int value)
    {
        this.value = value;
    }
}

void main()
{
    if(auto s = new S(10))
    {
        writeln(s.value);
    }
}

動いた。

トラックバック http://mikanya.dip.jp/memo/2010-02-23-2[an error occurred while processing this directive]