D中的Const结构

时间:2018-01-08 13:08:28

标签: struct const d

我正在尝试将结构作为编译时参数传递给函数。 我认为代码是自我解释的。我相当肯定这应该有效。但我不知道为什么它不会起作用。

void main(string[] args)
{
    const FooStruct fooStruct = FooStruct(5);

    barFunction!fooStruct();
}

public struct FooStruct()
{
    private const int value_;
    @property int value() { return value_; }

    this(int value) const
    {
        value_ = value;
    }
}

public static void barFunction(FooStruct fooStruct)
{
    fooStruct.value; /// do something with it.
}

1 个答案:

答案 0 :(得分:4)

    $sql = "SELECT patients.* , species.species_description
            FROM patients
            INNER JOIN species on patients.species_id=species.id;";

在这里,您声明FooStruct是一个模板结构,没有变量。如果这是你想要的,你需要在这一行上引用public struct FooStruct()

FooStruct!()

由于public static void barFunction(FooStruct fooStruct) 不接受任何模板参数,因此实际上并不需要对其进行模板化,您应该将其声明为:

FooStruct

执行此操作时,错误消息将更改为public struct FooStruct 。那是因为你正在调用可变构造函数。要解决此问题,请将第3行更改为constructor FooStruct.this (int value) const is not callable using argument types (int) const FooStruct fooStruct = const

最后,当您致电FooStruct(5);时,您尝试将barFunction作为模板参数(fooStruct)传递。由于barFunction!fooStruct()不是模板化函数,因此失败。你可能意味着barFunction

相关问题