Traits 特征 2.014

6.14 翻译

(d语言的反射,刚刚翻译了2/3了,浏览器异常退出,气死我。 6.14)

重来了
1.028里没有,谁能翻译一下

请参考:
C++ Traits [url]http://www.cnblogs.com/hush/archive/2004/03/10/2717.html[/url]
C++ Type traits [url]http://jjhou.csdn.net/programmer-6-type-traits-ddj.htm[/url]

Traits are extensions to the language to enable programs, at compile time, to get at information internal to the compiler. This is also known as compile time reflection. It is done as a special, easily extended syntax (similar to Pragmas) so that new capabilities can be added as required.

特征(Traits)是对程序语言的扩展,在编译期,对编译器内部的信息得到成为可能。 这也被称之为编译期反射。 它被作为特殊的,容易扩展的语法(类似于Pragmas ),这样新的能力能根据需要被添加。

TraitsExpression:
__traits ( TraitsKeyword , TraitsArguments )

TraitsKeyword:
isAbstractClass
isArithmetic
isAssociativeArray
isFinalClass
isFloating
isIntegral
isScalar
isStaticArray
isUnsigned
isVirtualFunction
isAbstractFunction
isFinalFunction
hasMember
getMember
getVirtualFunctions
classInstanceSize
allMembers
derivedMembers
isSame
compiles

TraitsArguments:
TraitsArgument
TraitsArgument , TraitsArguments

TraitsArgument:
AssignExpression
Type



isArithmetic

If the arguments are all either types that are arithmetic types, or expressions that are typed as arithmetic types, then true is returned. Otherwise, false is returned. If there are no arguments, false is returned.

如果变元是是算术类型,或者表达式是算术类型,那么返回true。 否则,返回false。 如果没有变元,返回false。

import std.stdio;

void main()
{
int i;
writefln(__traits(isArithmetic, int));
writefln(__traits(isArithmetic, i, i+1, int));
writefln(__traits(isArithmetic));
writefln(__traits(isArithmetic, int*));
}


Prints:

true
true
false
false


1、isFloating

Works like isArithmetic, except it's for floating point types (including imaginary and complex types).

像isArithmetic一样的工作,浮点类型除外(包括虚数和复数类型)。


2、isIntegral

Works like isArithmetic, except it's for integral types (including character types).

3、isScalar
Works like isArithmetic, except it's for scalar(标量?) types.

4、isUnsigned
Works like isArithmetic, except it's for unsigned无符号 types.

5、isStaticArray
Works like isArithmetic, except it's for static array types.

isAssociativeArray
Works like isArithmetic, except it's for associative关联 array types.


6、isAbstractClass

If the arguments are all either types that are abstract classes, or expressions that are typed as abstract classes, then true is returned. Otherwise, false is returned. If there are no arguments, false is returned.

如果变元是抽象类,或者表达式(的结果)是抽象类,那么返回true。 否则,返回false。 如果没有变元,返回false。

import std.stdio;

abstract class C { int foo(); }

void main()
{
C c;
writefln(__traits(isAbstractClass, C));
writefln(__traits(isAbstractClass, c, C));
writefln(__traits(isAbstractClass));
writefln(__traits(isAbstractClass, int*));
}


Prints:

true
true
false
false


7、isFinalClass

Works like isAbstractClass, except it's for final classes.
final classes出外


8、isVirtualFunction

Takes one argument. If that argument is a virtual function, true is returned, otherwise false.

接受一个变元。 如果那变元是一个虚函数,返回true,否则(返回)false。

import std.stdio;

struct S
{
void bar() { }
}

class C
{
void bar() { }
}

void main()
{
writefln(__traits(isVirtualFunction, C.bar)); // true
writefln(__traits(isVirtualFunction, S.bar)); // false
}



9、isAbstractFunction (抽象函数)

Takes one argument. If that argument is an abstract function, true is returned, otherwise false.

import std.stdio;

struct S
{
void bar() { }
}

class C
{
void bar() { }
}

class AC
{
abstract void foo();
}

void main()
{
writefln(__traits(isAbstractFunction, C.bar)); // false
writefln(__traits(isAbstractFunction, S.bar)); // false
writefln(__traits(isAbstractFunction, AC.foo)); // true
}



10、isFinalFunction

Takes one argument. If that argument is a final function, true is returned, otherwise false.

import std.stdio;

struct S
{
void bar() { }
}

class C
{
void bar() { }
final void foo();
}

final class FC
{
void foo();
}

void main()
{
writefln(__traits(isFinalFunction, C.bar)); // false
writefln(__traits(isFinalFunction, S.bar)); // false
writefln(__traits(isFinalFunction, C.foo)); // true
writefln(__traits(isFinalFunction, FC.foo)); // true
}



11、hasMember

The first argument is a type that has members, or is an expression of a type that has members. The second argument is a string. If the string is a valid property of the type, true is returned, otherwise false.

(接受两个变元)第一个变元是一个有成员的类型,或者表达式(的结果)是有成员的类型(class、struct等)。 第二个变元是一个字符串。 如果字符串是类型的一种有效的属性,返回true,否则(返回)false。

import std.stdio;

struct S
{
int m;
}

void main()
{ S s;

writefln(__traits(hasMember, S, "m")); // true
writefln(__traits(hasMember, s, "m")); // true
writefln(__traits(hasMember, S, "y")); // false
writefln(__traits(hasMember, int, "sizeof")); // true
}



12、getMember

Takes two arguments, the second must be a string. The result is an expression formed from the first argument, followed by a '.', followed by the second argument as an identifier.

接受两个变元,第二个必须是一个字符串。 结果是由第一个变元构成的表达式,其后是一‘.’,其后第二个变元是一个标识符。(象 s.mx 这样)


import std.stdio;

struct S
{
int mx;
static int my;
}

void main()
{ S s;

__traits(getMember, s, "mx") = 1; // same as s.mx=1;
writefln(__traits(getMember, s, "m" ~ "x")); // 1

__traits(getMember, S, "mx") = 1; // error, no this for S.mx
//mx对象实例成员,需要实例化后才能得到
__traits(getMember, S, "my") = 2; // ok
//my 是static的,是类成员。
}



13、getVirtualFunctions

The first argument is a class type or an expression of class type. The second argument is a string that matches the name of one of the functions of that class. The result is an array of the virtual overloads of that function.

第一个变元是一个class类型,或者class类型的表达式, 第二个变元是与类函数名(之一)匹配的字符串。 结果是有效重载函数的一个数组。(大概是把所有有效重载的函数名放在一个数组里)


import std.stdio;

class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 2; }
}

void main()
{
D d = new D();

foreach (t; __traits(getVirtualFunctions, D, "foo"))
writefln(typeid(typeof(t)));

alias typeof(__traits(getVirtualFunctions, D, "foo")) b;
foreach (t; b)
writefln(typeid(t));

auto i = __traits(getVirtualFunctions, d, "foo")[1](1);
writefln(i);
}



Prints:

void()
int()
void()
int()
2


14、classInstanceSize

Takes a single argument, which must evaluate to either a class type or an expression of class type. The result is of type size_t, and the value is the number of bytes in the runtime instance of the class type. It is based on the static type of a class, not the polymorphic type.

接受一个单一的变元,这必须对clss类型或者class类型表达式求值。 结果是类型size_t,而值是class类型的运行时实例中的字节数。 它基于一个类的静态类型,不是多态类型。


15、allMembers

Takes a single argument, which must evaluate to either a type or an expression of type. An array of string literals is returned, each of which is the name of a member of that type combined with all of the members of the base classes (if the class is a type). No name is repeated. Builtin properties are not included.

接受一个单一的变元,这必须对类型或者类型表达式求值。 返回一个(字符)串文字数组,

其中每个名称是由那个类型的(及其?)基类(直接继承的类?待测试)的所有成员构成。?(如果class是一个类型)。

没有名称被重复。 构建属性?不被包括在内。

import std.stdio;

class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 0; }
}

void main()
{
auto a = __traits(allMembers, D);
writefln(a);
// [_ctor,_dtor,foo,print,toString,toHash,opCmp,opEquals]
}


The order in which the strings appear in the result is not defined.
字符串在结果中出现的顺序没有定义

16、derivedMembers 派生成员

Takes a single argument, which must evaluate to either a type or an expression of type. An array of string literals is returned, each of which is the name of a member of that type. No name is repeated. Base class member names are not included. Builtin properties are not included.

接受一个单一的变元,这必须对类型或类型表达式求值。 返回一个字符串文字的数组,其中每个名称是那类型的一个成员。 名称不被重复。 基类成员名称不被包括。 构建属性不被包括。

import std.stdio;

class D
{
this() { }
~this() { }
void foo() { }
int foo(int) { return 0; }
}

void main()
{
auto a = __traits(derivedMembers, D);
writefln(a); // [_ctor,_dtor,foo]
}


The order in which the strings appear in the result is not defined.


17、isSame

Takes two arguments and returns bool true if they are the same symbol, false if not.

接受两个变元,如果他们是相同的符号,就返回true ,否则(返回)false。

import std.stdio;

struct S { }

int foo();
int bar();

void main()
{
writefln(__traits(isSame, foo, foo)); // true
writefln(__traits(isSame, foo, bar)); // false
writefln(__traits(isSame, foo, S)); // false
writefln(__traits(isSame, S, S)); // true
writefln(__traits(isSame, std, S)); // false
writefln(__traits(isSame, std, std)); // true
}


If the two arguments are expressions made up of literals or enums that evaluate to the same value, true is returned.

如果两个变元是有由文字literals或者枚举enums组成的表达式,那么计算到相同的值,就返回true。


18、compiles

Returns a bool true if all of the arguments compile (are semantically correct). The arguments can be symbols, types, or expressions that are syntactically correct. The arguments cannot be statements or declarations.

如果所有变元编译(是语义上正确的),返回true。 变元可以是句法上正确的的符号,类型,或者表达式。 变元不能是语句或者声明。

If there are no arguments, the result is false.

如果没有,结果是false

import std.stdio;

struct S
{
static int s1;
int s2;
}

int foo();
int bar();

void main()
{
writefln(__traits(compiles)); // false
writefln(__traits(compiles, foo)); // true
writefln(__traits(compiles, foo + 1)); // true
writefln(__traits(compiles, &foo + 1)); // false
writefln(__traits(compiles, typeof(1))); // true
writefln(__traits(compiles, S.s1)); // true
writefln(__traits(compiles, S.s3)); // false
writefln(__traits(compiles, 1,2,3,int,long,std)); // true
writefln(__traits(compiles, 3[1])); // false
writefln(__traits(compiles, 1,2,3,int,long,3[1])); // false
}



This is useful for:
这是有益的:

Giving better error messages inside generic code than the sometimes hard to follow compiler ones.
在代码内部有时能比努力的遵从编译器给出更好的错误信息。?

Doing a finer grained specialization than template partial specialization allows for.
允许比模板部分做的更精细更专业化。 ?


。。。。。。。。。。。。。
加上废掉的部分总共用了6个小时。 14:30完成
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值