如何声明 extern("C") 常量函数?
How to declare extern("C") const function?
我想编写 D 绑定。对于 class A
的生命,它的 someStruct *
成员变量永远不会改变,所以我想将它声明为 immutable
是很自然的。但是它的构造函数不会编译,除非我设法以某种方式将 APICall
函数的 return 值声明为 const
。怎么做?
struct someStruct;
const someStruct* APICall();
class A
{
this()
{
this.ptr = myfunc();
}
private:
immutable someStruct* ptr;
}
错误:function app.APICall without 'this' cannot be const
您想在 return 值周围使用括号:
const(someStruct*) APICall();
或者如果它永远不会改变,immutable
更好。 (const
主要用于函数参数而不是 return 值)
const or immutable without parentheses before or after a function declaration applies to the this
parameter,这就是错误说明它所说的原因:你正在尝试将它应用于 this
which没有。
但是,在执行此操作之前,请确保它实际上 是不可变的 - 指针永远不会改变,并且它指向的数据也永远不会改变。如果指针有任何可变性,你应该让它可变。
我想编写 D 绑定。对于 class A
的生命,它的 someStruct *
成员变量永远不会改变,所以我想将它声明为 immutable
是很自然的。但是它的构造函数不会编译,除非我设法以某种方式将 APICall
函数的 return 值声明为 const
。怎么做?
struct someStruct;
const someStruct* APICall();
class A
{
this()
{
this.ptr = myfunc();
}
private:
immutable someStruct* ptr;
}
错误:function app.APICall without 'this' cannot be const
您想在 return 值周围使用括号:
const(someStruct*) APICall();
或者如果它永远不会改变,immutable
更好。 (const
主要用于函数参数而不是 return 值)
const or immutable without parentheses before or after a function declaration applies to the this
parameter,这就是错误说明它所说的原因:你正在尝试将它应用于 this
which没有。
但是,在执行此操作之前,请确保它实际上 是不可变的 - 指针永远不会改变,并且它指向的数据也永远不会改变。如果指针有任何可变性,你应该让它可变。