我正在尝试为 重载运算符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 都认为我正在尝试使用他们的非标准块表达式。
海合会:
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 '{'