私はC++がenum
タイプのデフォルトoperator<<
機能を提供して見つける:結果を実行なぜC++は列挙型のデフォルトの "演算子"関数を提供していませんか?
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cout << t;
return 0;
}
は次のとおりです。
0
ている間は、デフォルトoperator>>
機能を提供していません:
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cin >> t;
return 0;
}
はそれを構築します次のコンパイルエラーが生成されます。
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>
......
なぜC++では列挙型のデフォルトのoperator>>
関数が提供されていませんか?
暗黙の変換は 'enum E'から 'int'まで存在しますが、' enum E& 'と' int& 'の間にはありません –
実際に列挙のための特別な' operator << 'はありません。代わりに列挙型は整数値に変換され、その整数値が使用されます。 –