の演算子をオーバーロードしようとしてstd::initializer_list
いますが、次のコードは GCC 4.7.2 でも Clang 3.2 でもコンパイルされません。
#include <initializer_list>
void operator+(const std::initializer_list<int>&, const std::initializer_list<int>&);
int main() {
{1, 2} + {3, 4};
}
13.5/6 では、演算子関数には、型がクラス、列挙型、またはいずれかへの参照であるパラメーターが少なくとも 1 つ必要であり、標準initializer_list
ではテンプレート クラスとして指定されているため、準拠する必要があるように思われます。ただし、明らかに Clang と GCC の両方が、非標準のブロック式を使用しようとしていると考えています。
GCC:
Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:7:8: warning: left operand of comma operator has no effect [-Wunused-value]
source.cpp:7:9: error: expected ';' before '}' token
source.cpp:7:9: warning: right operand of comma operator has no effect [-Wunused-value]
source.cpp:7:13: error: expected primary-expression before '{' token
source.cpp:7:13: error: expected ';' before '{' token
クラン:
Compilation finished with errors:
source.cpp:7:5: warning: expression result unused [-Wunused-value]
{1, 2} + {3, 4};
^
source.cpp:7:9: error: expected ';' after expression
{1, 2} + {3, 4};
^
;
source.cpp:7:8: warning: expression result unused [-Wunused-value]
{1, 2} + {3, 4};
^
source.cpp:7:13: error: expected expression
{1, 2} + {3, 4};
^
2 warnings and 2 errors generated.
これはコンパイルする必要がありますか?そうでない場合、なぜですか?
編集:
当然のことながら、VS 2012 の 11 月の CTP も失敗します。
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'