2015-08-27 22 views
5

SSCCE:Loại dàn diễn viên thất bại trong chuyển đổi cho enum với dung lượng hạn chế

enum class confirm {yes}; 

struct item 
{ 
    confirm s:4; // (1) limiting storage size required 
}; 

int main() 
{ 
    item itm; 

    itm.s = confirm::yes; // (2) OK 

    switch (itm.s) 
    { 
    case confirm::yes: // (3) Failure, need static data cast here? 
     break; 
    } 
} 

sản xuất lỗi:

In function ‘int main()’: 
error: could not convert ‘yes’ from ‘confirm’ to ‘int’ 
case confirm::yes: 
      ^

khi biên dịch với g ++ nhưng biên soạn tốt bởi kêu vang ++. Tại sao chuyển nhượng được đánh dấu bởi (2) có thể nhưng trường hợp khoản được đánh dấu bằng (3) không?

Cảnh báo về too small storageofftopic

+0

"Đây có phải là lỗi gcc" là câu trả lời đầy đủ không? (tự nhiên với bằng chứng rằng nó nên được cho phép) – Yakk

+0

@Yakk: có nghi ngờ – dyomas

+0

@dyp: cảm ơn bạn đã gắn thẻ, nhưng câu hỏi chỉ dành riêng cho 'lớp enum' —C++ 11; không có vấn đề như vậy trong các tiêu chuẩn trước đây – dyomas

Trả lời

2

này trông giống như một lỗi gcc, chúng ta có thể thấy công trình này in the latest gcc version:

Từ dự thảo C++ 11 phần chuẩn 6.4.2[stmt.switch]:

The condition shall be of integral type, enumeration type, or of a class type for which a single non-explicit conversion function to integral or enumeration type exists (12.3). [...] Integral promotions are performed. Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression : 

where the constant-expression shall be a converted constant expression (5.19) of the promoted type of the switch condition.

biểu thức liên tục được chuyển đổi được đề cập trong phần 5.19 có nội dung:

[...]A converted constant expression of type T is a literal constant expression, implicitly converted to type T, where the implicit conversion (if any) is permitted in a literal constant expression and the implicit conversion sequence contains only user-defined conversions, lvalue-to-rvalue conversions (4.1), integral promotions (4.5), and integral conversions (4.7) other than narrowing conversions (8.5.4). [ Note: such expressions may be used as case expressions (6.4.2), as enumerator initializers if the underlying type is fixed (7.2), and as integral or enumeration non-type template arguments (14.3). —end note ] [...]

Có lẽ điều này liên quan đến defect report 1767: Scoped enumeration in a switch statement. Vì vậy, có lẽ nó đã được thúc đẩy một int và sau đó so sánh trong trường hợp sẽ thất bại.

Các vấn đề liên quan