Re: 構造体の初期化
5件表示
すべてのコメント一覧へ
投稿ツリー
-
構造体の初期化 (tama, 2009/4/18 1:04)
-
Re: 構造体の初期化 (SHOO, 2009/4/18 2:35) «
-
Re: 構造体の初期化 (tama, 2009/4/18 20:38)
-
Re: 構造体の初期化 (SHOO, 2009/4/19 13:27)
-
Re: 構造体の初期化 (tama, 2009/4/22 17:25)
-
-
-
-
SHOO
投稿数: 658

構造体リテラルは { } じゃなくて、
#code(d){{{
a(VT("v at a", { } ));
}}}
{ } で初期化する方法は、構造体の静的初期化というらしいです。
静的メンバの初期化子はコンパイル時に評価可能なものに限られます
というのが仕様にあるので、デリゲートリテラル(実行時に評価)がダメなのでは?
#code(d){{{
auto s2 = ST("s2" , (string s) { return s; });
}}}
こうするとうまくいくみたいですよ。
D2.0の構造体の初期化は絶対に整理が必要だと思います。
#code(d){{{
import std.stdio;
struct X1
{
int x;
alias x this;
}
struct X2
{
int x;
alias x this;
this(int a)
{
x = a;
}
}
struct X3
{
int x;
alias x this;
this(int a)
{
x = a;
}
static X3 opCall(int a)
{
X3 ret;
ret.x = a * 2;
return ret;
}
}
struct X4
{
int x;
alias x this;
this(int a)
{
x = a;
}
void opAssign(int a)
{
x = a;
}
}
struct X5
{
int x;
alias x this;
this(int a)
{
x = a;
writefln("X5 constructor");
}
static X5 opCall(int a)
{
writefln("X5 opCall");
X5 ret;
ret.x = a * 2;
return ret;
}
void opAssign(int a)
{
x = a;
writefln("X5 opAssign");
}
}
void main()
{
X1 a = 10;
writefln(a.x); // 0
a = 20;
writefln(a.x); // 20
X2 b = 30;
writefln(b.x); // 0
X3 c = 40;
writefln(c.x); // 80
X4 d = 50;
writefln(d.x); // 0
X5 x5a = 1;// X5 opCall
X5 x5b = X5(2); // X5 constructor
X5 x5c = {3}; //
x5c = 4;// X5 opAssign
}
}}}
せっかくコンストラクタがあるのだから、初期化は全部コンストラクタに任せたらいいのに…混乱します。
あと初期化時に0になるのって仕様なのかな?(alias x thisがあるのに)
#code(d){{{
a(VT("v at a", { } ));
}}}
{ } で初期化する方法は、構造体の静的初期化というらしいです。
静的メンバの初期化子はコンパイル時に評価可能なものに限られます
というのが仕様にあるので、デリゲートリテラル(実行時に評価)がダメなのでは?
#code(d){{{
auto s2 = ST("s2" , (string s) { return s; });
}}}
こうするとうまくいくみたいですよ。
D2.0の構造体の初期化は絶対に整理が必要だと思います。
#code(d){{{
import std.stdio;
struct X1
{
int x;
alias x this;
}
struct X2
{
int x;
alias x this;
this(int a)
{
x = a;
}
}
struct X3
{
int x;
alias x this;
this(int a)
{
x = a;
}
static X3 opCall(int a)
{
X3 ret;
ret.x = a * 2;
return ret;
}
}
struct X4
{
int x;
alias x this;
this(int a)
{
x = a;
}
void opAssign(int a)
{
x = a;
}
}
struct X5
{
int x;
alias x this;
this(int a)
{
x = a;
writefln("X5 constructor");
}
static X5 opCall(int a)
{
writefln("X5 opCall");
X5 ret;
ret.x = a * 2;
return ret;
}
void opAssign(int a)
{
x = a;
writefln("X5 opAssign");
}
}
void main()
{
X1 a = 10;
writefln(a.x); // 0
a = 20;
writefln(a.x); // 20
X2 b = 30;
writefln(b.x); // 0
X3 c = 40;
writefln(c.x); // 80
X4 d = 50;
writefln(d.x); // 0
X5 x5a = 1;// X5 opCall
X5 x5b = X5(2); // X5 constructor
X5 x5c = {3}; //
x5c = 4;// X5 opAssign
}
}}}
せっかくコンストラクタがあるのだから、初期化は全部コンストラクタに任せたらいいのに…混乱します。
あと初期化時に0になるのって仕様なのかな?(alias x thisがあるのに)
投票数:166
平均点:5.42
返信する