<分区>
我正在尝试为 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 规定运算符函数应至少有一个参数,其类型为类、枚举或对其中之一的引用,标准将 initializer_list
指定为模板类,所以对我来说它似乎应该是一致的。但是,显然 Clang 和 GCC 都认为我在尝试使用他们的非标准 block 表达式。
海湾合作委员会:
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
clang :
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 '{'