5

我正在尝试使用以下代码从类中获取完整数据成员的列表:

import std.stdio;
import std.traits;

class D
{
    static string[] integralMembers = getIntegralMembers();

    static string[] getIntegralMembers()
    {
        auto allMembers = __traits(allMembers, D);

        string[] tmp = new string[allMembers.length];
        int ct = 0;

        for(int i = 0; i != allMembers.length; ++i) {
            bool isInteg = __traits(isIntegral, __traits(getMember, D, allMembers[i]));
            if(isInteg) {
                tmp[ct++] = allMembers[i];
            }
        }

        string[] ret = new string[ct];

        for(int i = 0; i != ct; ++i) {
            ret[i] = tmp[i];
        }

        return ret;
    }

    int a;
    this() { }
    ~this() { }
}

void main()
{
    auto integralMembers = D.integralMembers;

    foreach(mem; integralMembers)
    {
        writeln(mem);
    }
}

但是,编译失败并出现以下错误:

main.d(17): Error: variable i cannot be read at compile time
main.d(17): Error: expression expected as second argument of __traits getMember
main.d(19): Error: variable i cannot be read at compile time
main.d(7):        called from here: getIntegralMembers()

如何使此代码编译?

4

1 回答 1

7

即使该函数仅在该程序内的编译期间运行,它仍必须可编译为可以在运行时运行的函数。

  1. 您需要声明allMembers为清单常量:

    enum allMembers = __traits(allMembers, D);
    

    allMembers是一个元组。如果您使用auto,它将作为字符串元组保存在“堆栈”上,成为运行时值,因此无法在编译时评估__traits.

  2. 您需要使用foreach而不是for. foreachover a tuple 的特殊之处在于它将静态展开,因此索引(和值)将可以访问__traits

固定程序:

import std.stdio;
import std.traits;

class D
{
    static string[] integralMembers = getIntegralMembers();

    static string[] getIntegralMembers()
    {
        enum allMembers = __traits(allMembers, D);

        string[] tmp = new string[allMembers.length];
        int ct = 0;

        foreach(i, member; allMembers) {
            bool isInteg = __traits(isIntegral, __traits(getMember, D, member));
            if(isInteg) {
                tmp[ct++] = allMembers[i];
            }
        }

        string[] ret = new string[ct];

        for(int i = 0; i != ct; ++i) {
            ret[i] = tmp[i];
        }

        return ret;
    }

    int a;
    this() { }
    ~this() { }
}

void main()
{
    auto integralMembers = D.integralMembers;

    foreach(mem; integralMembers)
    {
        writeln(mem);
    }
}
于 2014-10-06T23:34:05.290 回答