操作符可以重载initializer_list文字? [英] Can operators be overloaded for initializer_list literals?

查看:173
本文介绍了操作符可以重载initializer_list文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为 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认为我试图使用他们的非标准块表达式。



GCC:

 编译完成时出现错误:
source.cpp:在函数'int main()'中:
source.cpp:7:8:warning:left operand的逗号运算符没有效果[-Wunused-value]
source.cpp:7:9:error:expected';'before'} token
source.cpp:7:9:warning:right逗号运算符的操作数没有效果[-Wunused-value]
source.cpp:7:13:错误:'{'token
source.cpp:7:13:error:预期';'before'{'token

Clang:

 编译完成时出现错误:
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:预期表达式
{1,2} + {3,4}
^
生成2个警告和2个错误。

这应该编译吗?



不奇怪的是, VS 2012的11月CTP失败:

 错误C2143:语法错误:缺少';'before'}'
错误C2059:语法错误:'{'
错误C2143:语法错误:缺少';'before'{'


解决方案

据我理解13.5 / 6,


函数应为非静态成员函数或非成员函数,并且具有
至少一个参数,其类型为类,对类的引用,枚举或对
枚举


这不可能。支撑初始化列表与 std :: initializer_list 不同,它们不可互换。以下应该可以使用:

  std :: initializer_list< int>({1,2})+ std :: initializer_list< ; int>({2,1})

或者:

 运算符+({1,2},{2,1})

更新:要澄清的是,语法语法中没有规则允许支持初始化列表出现在OP建议的上下文中。 Braced-init-lists只能出现在一些­ thing即将被初始化的上下文中。


I am trying to overload operators for std::initializer_list, but the following compiles neither in GCC 4.7.2 nor 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 says that an operator function shall have at least one parameter whose type is a class, enum, or reference to either, and the standard specifies initializer_list as a template class, so to me it seems like it should be conformant. However, apparently both Clang and GCC think I'm trying to use their non-standard block expressions.

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

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.

Should this compile? If not, why not?

EDIT:

And not surprisingly, the November CTP of VS 2012 fails as well:

error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'

解决方案

As far as I understand 13.5/6,

An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration

this should not be possible. Braced-init-lists are not the same as std::initializer_lists, and they're not interchangeable. The following should work, though:

std::initializer_list<int>({1,2}) + std::initializer_list<int>({2,1})

Or this:

operator+({1,2}, {2,1})

Update: To clarify, the point is that there's no rule in the language grammar that allows a braced-init-list to appear in the context suggested by the OP. Braced-init-lists can only appear in contexts where some­thing is about to be initialized, if you will.

这篇关于操作符可以重载initializer_list文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆