I am searching for some help in next situation:
I have some class and some method in it, syntax is like this:
class SomeClass {
public:
void doSomething(int *a);
};
So I want to call this method like
SomeClass::doSomething({ 0, 1, 2, 3, 4 });
Is it possible in any language?
Any (C++, C, obj-c, obj-c++) implementation is welcome!
I know that this initialization block is a body of array, like
int *a = { 0, 1, 2, 3, 4 };
SomeClass::doSomething(a);
But interface will look great, I think, if there will be no temp variables before function calls (as we don’t need to know the type of parameter in class-client). So, is there any chance to make this?
This is about C++11 initializer lists (section 18.9).
Only the compiler can create an initializer list, but you can treat it like a standard STL-style container with
begin(),end(),size(), and random-access iterators.std::vector(and I expect some other containers) can now be constructed with initializer lists, sois equivalent to
except that it may perform fewer allocations. Note that the
const char*have been turned intostd::stringautomagically.