2017-07-21 17 views
6

tôi thấy C++ cung cấp các chức năng mặc định operator<< cho enum loại:Tại sao C++ không cung cấp chức năng "operator >>" mặc định cho kiểu enum?

#include <iostream> 
using namespace std; 

enum OpType { 
    Select, 
    Insert 
}; 

int main() { 
    OpType t = Select; 
    cout << t; 
    return 0; 
} 

kết quả Chạy là:

0 

Trong khi không cung cấp mặc định operator>> chức năng:

#include <iostream> 
using namespace std; 

enum OpType { 
    Select, 
    Insert 
}; 

int main() { 
    OpType t = Select; 
    cin >> t; 
    return 0; 
} 

Build nó sẽ tạo ra các lỗi biên dịch sau:

prog.cpp: In function ‘int main()’: 
prog.cpp:11:6: error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘OpType’) 
    cin >> t; 
    ~~~~^~~~ 
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:168:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match> 
     operator>>(bool& __n) 
     ^~~~~~~~ 
/usr/include/c++/6/istream:168:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:172:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match> 
     operator>>(short& __n); 
     ^~~~~~~~ 
/usr/include/c++/6/istream:172:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short int&’ from an rvalue of type ‘short int’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:175:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match> 
     operator>>(unsigned short& __n) 
     ^~~~~~~~ 
/usr/include/c++/6/istream:175:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short unsigned int&’ from an rvalue of type ‘short unsigned int’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:179:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match> 
     operator>>(int& __n); 
     ^~~~~~~~ 
/usr/include/c++/6/istream:179:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:182:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match> 
     operator>>(unsigned int& __n) 
     ^~~~~~~~ 
/usr/include/c++/6/istream:182:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘unsigned int&’ from an rvalue of type ‘unsigned int’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:186:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match> 
     operator>>(long& __n) 
     ^~~~~~~~ 
/usr/include/c++/6/istream:186:7: note: conversion of argument 1 would be ill-formed: 
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘long int&’ from an rvalue of type ‘long int’ 
    cin >> t; 
     ^
In file included from /usr/include/c++/6/iostream:40:0, 
       from prog.cpp:1: 
/usr/include/c++/6/istream:190:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match> 
...... 

Tại sao C++ không cung cấp chức năng mặc định operator>> cho loại enum?

+5

chuyển đổi ngầm tồn tại từ 'enum E' thành' int', nhưng không phải giữa 'enum E &' và 'int &' –

+2

Thực ra không có toán tử 'đặc biệt <<' nào cho liệt kê. Thay vào đó, việc liệt kê được chuyển thành một giá trị số nguyên, và giá trị số nguyên đó sau đó được sử dụng. –

Trả lời

2

Thậm chí bỏ qua thực tế là không có toán tử << hoặc - enum được chuyển hoàn toàn thành số nguyên đầu tiên - không rõ ràng làm thế nào nó có thể được chuẩn hóa.

Kết quả của việc đọc giá trị enum không tồn tại là lỗi, không xác định, được xác định thực hiện hay chưa xác định?

Các ứng dụng khác nhau sẽ muốn có hành vi khác nhau, có lẽ ngay cả trong cùng một ứng dụng.
Và điều đó đặt mọi người trở lại hình vuông, viết cùng mã như chúng tôi làm ngay bây giờ. Ngoài ra còn có các biến chứng mà các nhà khai thác dòng không phải là một phần của ngôn ngữ chính nó, và không phải là khái niệm dòng, vì vậy bạn có thể không thực sự có trình biên dịch tạo ra chúng cho bạn.

-3

Không có operator << lấy enum, bạn đang gọi số lượng quá tải khi chuyển đổi ngầm định giá trị enum thành số nguyên. Lý do cho điều này là số liệu quá tải operator <<operator >> lấy số nguyên (và các yếu tố đầu vào khác) chỉ là các hàm bình thường, không phải là toán tử tích hợp. Việc cung cấp phương thức chung cho các kiểu do người dùng định nghĩa (chẳng hạn như enums) là không thể ngay bây giờ vì nó sẽ đòi hỏi sự phản chiếu thời gian biên dịch của một loại nào đó.

+0

"Cung cấp phương pháp chung cho các loại do người dùng xác định (chẳng hạn như enums) là không thể ngay bây giờ vì nó sẽ yêu cầu phản ánh thời gian biên dịch của một số loại." Điều này thực sự rất có thể vào thời điểm này. Đối với enums, nó hoàn toàn có thể, vì chúng có thể chuyển đổi thành/từ các kiểu số nguyên. – Justin

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