可以重载花括号吗? [英] Possible to overload curly braces?

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

问题描述

我几乎可以肯定不是,但我没有找到一个明确的答案:

I'm almost sure that not, but I didn't find a difinitive answer to that:

是否可以重载花括号?如:

Is it possible to overload the curly braces? As in:

class Foo
{
    int i;
    public:
    int operator{}(int _i){return _i+42;};    
};

int main()
{
    Foo f;
    f{2};
    return 0;
}

它从未被提及,既不允许也不不允许.gcc 4.6 不编译它,但这并不意味着标准不允许它,对吧?

It is never mentioned, neither as allowed nor as not allowed. gcc 4.6 doesn't compile it, but that doesn't mean it is not allowed by the standard, right?

推荐答案

在 C++ 中,花括号 {} 不是与 [](数组下标运算符)或()(函数调用运算符),所以它们不能被重载.

In C++ curly braces {} are not operators unlike the [] (Array subscripting operator) or () (Function call operator), so they can't be overloaded.

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

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