问题
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