为什么是这样constexpr
static
成员函数,由//! Nah
评论,不被视为constexpr
什么时候打电话?
struct Item_id
{
enum Enum
{
size, position, attributes, window_rect, max_window_size, _
};
static constexpr int n_items_ = _; // OK
constexpr auto member_n_items() const -> int { return _; } // OK
static constexpr auto static_n_items() -> int { return _; } // OK
static constexpr int so_far = n_items_; // OK
#ifndef OUT_OF_CLASS
static constexpr int bah = static_n_items(); //! Nah.
#endif
};
constexpr auto n_ids() -> int { return Item_id().member_n_items(); } // OK
auto main() -> int
{
#ifdef OUT_OF_CLASS
static constexpr int bah = Item_id::static_n_items(); // OK
#endif
}
MinGW g++ 5.1 报告
constexpr.cpp:12:46: error: 'static constexpr int Item_id::static_n_items()' called in a constant expression
static constexpr int bah = static_n_items(); //! Nah.
Visual C++ 2015 报告
constexpr.cpp(12): error C2131: expression did not evaluate to a constant
constexpr.cpp(12): note: failure was caused by call of undefined function or one not declared 'constexpr'
constexpr.cpp(12): note: see usage of 'Item_id::static_n_items'
我的文本编辑器坚持要求调用中的名称与函数定义中的名称相同。
这似乎与不完整的课程有关,因为OUT_OF_CLASS
定义它编译得很好。
但那为什么n_items_
数据有效,为什么这样的规则(对我来说没有意义)?