Possible to overload curly braces?

左心房为你撑大大i 提交于 2021-01-27 18:30:16

问题


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;
}

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?


回答1:


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



来源:https://stackoverflow.com/questions/10528320/possible-to-overload-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!