Is this the best way to make the c operator in C ++?

Edit:

So, this question has been misinterpreted to such a ridiculous degree that it no longer makes sense. I donโ€™t know how, since the question I really asked was whether my specific implementation of this โ€œyesโ€ was known to be pointless, yes, not remotely resembling the C ++ idiomatic macro and - is it necessary use auto , or if there was a suitable workaround. He should not have paid much attention to this, and, of course, there was no such misunderstanding on such a scale. It makes no sense to ask the respondents to edit their answers, I donโ€™t want someone to lose their reputation on this issue, and there is some good information for potential potential viewers, so Iโ€™m going to arbitrarily choose one of the below voted answers to evenly distribute the reputation involved. Move on, nothing is visible here.


I saw this question and decided it would be interesting to write a with statement in C ++. The auto keyword makes this very easy, but is there a better way to do this, perhaps without using auto ? For brevity, I have reduced some bits of code a bit.

 template<class T> struct with_helper { with_helper(T& v) : value(v), alive(true) {} T* operator->() { return &value; } T& operator*() { return value; } T& value; bool alive; }; template<class T> struct with_helper<const T> { ... }; template<class T> with_helper<T> make_with_helper(T& value) { ... } template<class T> with_helper<const T> make_with_helper(const T& value) { ... } #define with(value) \ for (auto o = make_with_helper(value); o.alive; o.alive = false) 

Here is an example (updated) of use with a more typical case, which shows the use of with , as it is found in other languages.

 int main(int argc, char** argv) { Object object; with (object) { o->member = 0; o->method(1); o->method(2); o->method(3); } with (object.get_property("foo").perform_task(1, 2, 3).result()) { std::cout << (*o)[0] << '\n' << (*o)[1] << '\n' << (*o)[2] << '\n'; } return 0; } 

I chose o because it is an unusual identifier, and its form gives the impression of a โ€œcommon thing." If you have an idea for a better identifier or more usable syntax, please suggest it.

+4
source share
4 answers

?? vb syntax attempt in c ++

with says all things in the next block default to the object I said to do it right? Performs a series of statements repeating a reference to a single object or structure.

 with(a) .do .domore .doitall 

so how does the example give you the same syntax?

for me examples of why use a, where are some link references

but not

 book.sheet.table.col(a).row(2).setColour book.sheet.table.col(a).row(2).setFont book.sheet.table.col(a).row(2).setText book.sheet.table.col(a).row(2).setBorder 

you have

 with( book.sheet.table.col(a).row(2) ) .setColour .setFont .setText .setBorder 

seems like the same simple and more common syntax in C ++ for

 cell& c = book.sheet.table.col(a).row(2); c.setColour c.setFont c.setText c.setBorder 
+6
source

If you use auto , why use macros at all?

 int main() { std::vector<int> vector_with_uncommonly_long_identifier; { auto& o = vector_with_uncommonly_long_identifier; o.push_back(1); o.push_back(2); o.push_back(3); } const std::vector<int> constant_duplicate_of_vector_with_uncommonly_long_identifier (vector_with_uncommonly_long_identifier); { const auto& o = constant_duplicate_of_vector_with_uncommonly_long_identifier; std::cout << o[0] << '\n' << o[1] << '\n' << o[2] << '\n'; } { auto o = constant_duplicate_of_vector_with_uncommonly_long_identifier.size(); std::cout << o <<'\n'; } } 

EDIT: Without auto just use typedef and references.

 int main() { typedef std::vector<int> Vec; Vec vector_with_uncommonly_long_identifier; { Vec& o = vector_with_uncommonly_long_identifier; o.push_back(1); o.push_back(2); o.push_back(3); } } 
+7
source

For C ++ 0x (which you assume):

 int main() { std::vector<int> vector_with_uncommonly_long_identifier; { auto& o = vector_with_uncommonly_long_identifier; o.push_back(1); o.push_back(2); o.push_back(3); } } 
+3
source

Why not just use a good lambda?

 auto func = [&](std::vector<int>& o) { }; func(vector_with_a_truly_ridiculously_long_identifier); 

The simple fact is that if your identifiers are so long that you cannot enter them every time, use a link, function, pointer, etc. to solve this problem or, better, reorganize the name. Similar expressions (e.g. using () in C #) have additional side effects (deterministic cleanup, in my example). Your statement in C ++ has no significant factual advantages, because in fact it does not cause any additional behavior against just writing code.

0
source

All Articles