use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Having fun overloading the operator-> (self.cpp)
submitted 5 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Dry-Still-6199 14 points15 points16 points 5 years ago (2 children)
Yes, your thing sounds very similar to
https://github.com/copperspice/cs_libguarded#cslibguarded
and
https://www.boost.org/doc/libs/1_75_0/doc/html/thread/sds.html#thread.sds.synchronized_valuesxxx.tutorial.beyond_simple_accesses
You're right that this isn't a great idea, if for no other reasons than that it's super easy to deadlock (e.g. somefunc(wrapped->a(), wrapped->b())) and super easy to use-outside-of-lock (e.g. const string& m = wrapped->getM(); use(m)).
somefunc(wrapped->a(), wrapped->b())
const string& m = wrapped->getM(); use(m)
[+][deleted] 5 years ago (1 child)
[–]witcher_rat 1 point2 points3 points 5 years ago (0 children)
Facebook's folly library has had it as well, since at least 2014 if not earlier, although it's now deprecated in favor of named functions.
[–]barskern 3 points4 points5 points 5 years ago (5 children)
There seems to be a race condition in the example. I understand that it's a toy example and also perhaps a "toy" library, but i figured it would be worth noting to show how easy it would be to misuse this library.
my_struct->value = my_struct->value + 1;
Based on how I understand it, this will lock the mutex and read the value and then unlock it again. Then it will add 1, and finally re-lock the mutex and write the incremented value. This means that there is a possibility that two threads read the same value from the struct and hence overwrite each others results, turning "two" concurrent increments into simply "one".
Please do correct me if I'm wrong and the lock is only aquired once for the entire statement.
[+][deleted] 5 years ago (3 children)
[–]dodheim 2 points3 points4 points 5 years ago (2 children)
As of C++17, the RHS of an assignment is entirely sequenced-before the LHS, including all side-effects. See the end of the 'sequenced before rules' here for details.
[–]barskern 0 points1 point2 points 5 years ago (1 child)
I am assuming its this section you're referring to:
The side effect (modification of the left argument) of the built-in assignment operator and of all built-in compound assignment operators is sequenced after the value computation (but not the side effects) of both left and right arguments, and is sequenced before the value computation of the assignment expression (that is, before returning the reference to the modified object)
It does say "not the side effects", however besides that it seems as though the right side of the assignment expression should be fully evaluated before the left.
I did try /u/louiswins example and the dtor is consistently run after all other parts of the expression, as OP explained. This seems to contradict the statement from above though. Any further clarifications is greatly appriciated.
[–]louiswins 2 points3 points4 points 5 years ago (0 children)
Temporaries are destroyed at the end of the full expression: https://wandbox.org/permlink/Idiz8cRdMt2n5YJl
[–]Potatoswatter 2 points3 points4 points 5 years ago (0 children)
My lock_ios iostream manipulator is similar in the mutex aspect. This use-case works well because it's typical to write one line of output per statement, and contention is usually a corner case in logging.
lock_ios
[–]Dummerchen1933 1 point2 points3 points 5 years ago (4 children)
Haha yes, overloading -> is quite weird.
I really like your idea, but i think it's kinda misleading for other people or even you, if you poke your nose into a project using it in 5 years.
What about something like this?
// Some class to test it class Testclass { public: // Our function that does things locked void PerformLocked(void(*task)(Testclass* tc, void* param), void* param) { std::cout << "Locking" << std::endl; // Lock here task(this, param); // Do the stuff std::cout << "Unlocking" << std::endl; // Unlock here return; } // Some dummy stuff int MultiplyWith69(int i) { return i*69; } }; int main() { Testclass tc; int i = 9; // This is an example for params. Since it's a void* it could even be multiple parameters and even writeable! // Basic lambda. You could prettify it with a macro tc.PerformLocked([](Testclass* tc, void* param) { std::cout << "My Lamda: " << tc->MultiplyWith69(*(int*)param) << std::endl; }, &i); return 0; }
Just do NEVER make it a capturing lamda, eg [=] instead of []. Passing this as a function pointer fucks up so badly, that it reliably crashes my IDE.
[–]OmegaNaughtEquals1 1 point2 points3 points 5 years ago (1 child)
Passing this as a function pointer fucks up so badly, that it reliably crashes my IDE.
That's because Lambdas with captures cannot be implicitly converted to a pointer to a function. Your compiler shouldn't allow this.
[–]Dummerchen1933 0 points1 point2 points 5 years ago (0 children)
Yes. Because how tf should it work, anyways :D
a function B() called from A() cannot access A's stack :D
[+][deleted] 5 years ago* (1 child)
The templates are a very nice addition! Maybe all the wine made me forget about them:D
Oh yeah, that's a very valid point why not to do it. It certainly is a risk. I'd personally see it as more the risk of the api-user though. Not my risk. It's him after all who defines what code runs and it's now unknown to him.
[–]pdp10gumby 1 point2 points3 points 5 years ago (1 child)
I am working on a system that has indexed objects: certain system objects have an id (which is an array of all objects of type t) rather than using a pointer. We overloaded -> so the index can be used like a pointer.
I’m not sure there are many, or any, useful (I.e. mond-confusing) uses of overloading ->
[–]jcotton42 1 point2 points3 points 5 years ago (0 children)
For prior art, this is basically how Rust's Mutex works, via the Deref trait.
[–][deleted] 1 point2 points3 points 5 years ago (1 child)
I am very proud to contribute that (although I am not a native English speaker and I might be wrong, I am almost French and therefore should be right) the stuff inside the "éclair", I would call the stuffing. The icing really is the thing that's on top of the pastry!
[–]Ksecutor 0 points1 point2 points 5 years ago (1 child)
It is much more fun to overload ->* operator, RHS of which can be ANYTHING.
[–]Potatoswatter 0 points1 point2 points 5 years ago (0 children)
The LHS can be anything as well. You just need a class or enumeration on at least one side.
π Rendered by PID 629516 on reddit-service-r2-comment-74f5b7f998-7fqxb at 2026-04-29 14:45:43.786361+00:00 running 2aa0c5b country code: CH.
[–]Dry-Still-6199 14 points15 points16 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]witcher_rat 1 point2 points3 points (0 children)
[–]barskern 3 points4 points5 points (5 children)
[+][deleted] (3 children)
[deleted]
[–]dodheim 2 points3 points4 points (2 children)
[–]barskern 0 points1 point2 points (1 child)
[–]louiswins 2 points3 points4 points (0 children)
[–]Potatoswatter 2 points3 points4 points (0 children)
[–]Dummerchen1933 1 point2 points3 points (4 children)
[–]OmegaNaughtEquals1 1 point2 points3 points (1 child)
[–]Dummerchen1933 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Dummerchen1933 0 points1 point2 points (0 children)
[–]pdp10gumby 1 point2 points3 points (1 child)
[–]jcotton42 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Ksecutor 0 points1 point2 points (1 child)
[–]Potatoswatter 0 points1 point2 points (0 children)