初始化成员函数字段
Initializing member functions fields
在阅读名为 Abominable functions 的 C++1z 论文时,我发现了以下代码:
class rectangle {
public:
using int_property = int() const; // common signature for several methods
int_property top;
int_property left;
int_property bottom;
int_property right;
int_property width;
int_property height;
// Remaining details elided
};
我以前从未见过这样的代码(论文本身说找到这样的代码很奇怪)所以我想尝试一下这种方法并给那些 int_property
:
class rectangle {
int f() const { return 0; }
public:
using int_property = int() const; // common signature for several methods
int_property top = f; // <--- error!
int_property left;
int_property bottom;
int_property right;
int_property width;
int_property height;
// Remaining details elided
};
在我上面的修改中,编译器抱怨 f
说 (only '= 0' is allowed) before ';' token
;我的其他尝试是:
class rectangle {
int f() const { return 0; }
public:
using int_property = int() const; // common signature for several methods
// invalid initializer for member function 'int rectangle::top() const'
int_property top{f};
int_property left{&f};
// invalid pure specifier (only '= 0' is allowed) before ';' token
int_property bottom = f;
int_property right = &f;
int_property width;
int_property height;
// class 'rectangle' does not have any field named 'width'
// class 'rectangle' does not have any field named 'height'
rectangle() : width{f}, height{&rectangle::f} {}
};
所以问题是:
- 我应该怎么做才能使所有
int_property
“字段”都指向一个函数?
- 如何为所有
int_property
“字段”赋值?
该论文在 2015 年 11 月 10 日作为提案 #P0172R0 引入 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/ . I believe you are using compiler that is not supporting this functionality currently, but you can check later :). Also it might be interesting for you to read current standard http://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf,或者至少检查您的编译器当前支持的功能。
int() const
是一个带有 cv-qualifier 的函数类型。声明 int_property top;
声明了一个函数,而不是一个变量。此声明与 int top() const;
.
具有相同的效果
与其他成员函数一样,您可以通过提供函数定义来定义它们。
int rectangle::top() const {
return 0;
}
在阅读名为 Abominable functions 的 C++1z 论文时,我发现了以下代码:
class rectangle {
public:
using int_property = int() const; // common signature for several methods
int_property top;
int_property left;
int_property bottom;
int_property right;
int_property width;
int_property height;
// Remaining details elided
};
我以前从未见过这样的代码(论文本身说找到这样的代码很奇怪)所以我想尝试一下这种方法并给那些 int_property
:
class rectangle {
int f() const { return 0; }
public:
using int_property = int() const; // common signature for several methods
int_property top = f; // <--- error!
int_property left;
int_property bottom;
int_property right;
int_property width;
int_property height;
// Remaining details elided
};
在我上面的修改中,编译器抱怨 f
说 (only '= 0' is allowed) before ';' token
;我的其他尝试是:
class rectangle {
int f() const { return 0; }
public:
using int_property = int() const; // common signature for several methods
// invalid initializer for member function 'int rectangle::top() const'
int_property top{f};
int_property left{&f};
// invalid pure specifier (only '= 0' is allowed) before ';' token
int_property bottom = f;
int_property right = &f;
int_property width;
int_property height;
// class 'rectangle' does not have any field named 'width'
// class 'rectangle' does not have any field named 'height'
rectangle() : width{f}, height{&rectangle::f} {}
};
所以问题是:
- 我应该怎么做才能使所有
int_property
“字段”都指向一个函数? - 如何为所有
int_property
“字段”赋值?
该论文在 2015 年 11 月 10 日作为提案 #P0172R0 引入 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/ . I believe you are using compiler that is not supporting this functionality currently, but you can check later :). Also it might be interesting for you to read current standard http://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf,或者至少检查您的编译器当前支持的功能。
int() const
是一个带有 cv-qualifier 的函数类型。声明 int_property top;
声明了一个函数,而不是一个变量。此声明与 int top() const;
.
与其他成员函数一样,您可以通过提供函数定义来定义它们。
int rectangle::top() const {
return 0;
}