Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C++ provide default "operator>>" function for enum type?

Tags:

c++

enums

I find C++ provides the default operator<< function for enum type:

#include <iostream>
using namespace std;

enum OpType {
    Select,
    Insert
};

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

Running result is:

0

While doesn't provide default operator>> function:

#include <iostream>
using namespace std;

enum OpType {
    Select,
    Insert
};

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

Build it will generate following compile errors:

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>
......

Why doesn't C++ provide default operator>> function for enum type?

like image 218
Nan Xiao Avatar asked Jul 21 '17 06:07

Nan Xiao


People also ask

What is the default type of enum?

enum values are fixed. enum can be displayed as a string and processed as an integer. The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.

What is the default type of enum in C++?

The type of a C++ enum is the enum itself. Its range is rather arbitrary, but in practical terms, its underlying type is an int . It is implicitly cast to int wherever it's used, though.

Does enum class exist in C?

In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used.


1 Answers

Even disregarding the fact that there's no << operator either — the enum is implicitly converted to an integer first — it's not obvious how it could be standardised.

Would the result of reading an enum value that doesn't exist be an error, unspecified, implementation-defined, or undefined?

Different applications will want different behaviour, perhaps even within the same application.
And that puts everyone back at square one, writing the same code as we do now.

There's also the complication that the stream operators aren't part of the language itself, and neither is the stream concept, so you can't really have the compiler generate them for you.

like image 93
molbdnilo Avatar answered Oct 03 '22 06:10

molbdnilo