2017-07-21 2 views
6

私は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>>関数が提供されていませんか?

+5

暗黙の変換は 'enum E'から 'int'まで存在しますが、' enum E& 'と' int& 'の間にはありません –

+2

実際に列挙のための特別な' operator << 'はありません。代わりに列挙型は整数値に変換され、その整数値が使用されます。 –

答えて

2

<<演算子がないという事実を無視しても、enumは暗黙的に整数に変換されます。これはどのようにして標準化できるかはわかりません。

存在しないenum値を読み取った結果が、エラー、未指定、実装定義、または未定義になりますか?

異なるアプリケーションでは、同じアプリケーション内であっても、異なる動作が必要になります。
これは皆が正方形に戻って、私たちが今行っているのと同じコードを書いています。

ストリーム演算子は言語そのものではなく、どちらもストリームの概念ではないので、実際にコンパイラで生成することはできません。

-3

列挙型を取っているoperator <<はありません。整数に整数値を暗黙的に変換してオーバーロードを行う整数を呼び出しています。その理由は、整数(および他の入力)を取るオーバーロードは、組み込み演算子ではなく通常の関数にすぎないということです。コンパイル時に何らかの種類のリフレクションが必要になるため、ユーザー定義型(enumなど)の汎用メソッドを提供することは現時点では不可能です。

+0

"コンパイル時に何らかの種類のリフレクションが必要になるため、ユーザー定義型(enumなど)の汎用メソッドを提供することはできません。これは実際には現時点では非常に可能です。列挙型の場合、整数型との間で変換が可能なので、完全に可能です。 – Justin

関連する問題