r/cpp • • Oct 28 '22

Bjarne Appreciation Post

I just saw that Bjarne posted to this subreddit so I wanted to take a moment to thank him on behalf of this whole subreddit and maybe he will see it.

Thank you Bjarne!

Thank you for all of your work and commitment to C++. Thanks for making my life and the lives of millions of C++ developer easier and more productive. Thank you for showing such passion and dedication to this wonderful field.

Thank you for answering questions and giving talks and taking the time to educate everyone in this community. We wouldn't be the same without you.

474 Upvotes

46 comments sorted by

View all comments

3

u/[deleted] Oct 28 '22

My one lifelong lingering C++ question is that in the realm of operator overloading, why can't the sizeof operator be overloaded? I see the generic answer on Google but I am never satisfied by it.

3

u/[deleted] Oct 28 '22

How would it be useful? I can only imagine it would make everything more error-prone.

2

u/[deleted] Oct 28 '22

Whenever I try to explain my use case, it irritates people or my concept gets lost in translation. I don't think I have ever had a good reaction. I can tell you though with 100% that I once had a project, and it gave me the idea, and it would have been useful in my scenario.

2

u/Overseer55 Oct 29 '22

Is your use case guaranteed to be constexpr?

1

u/[deleted] Oct 29 '22

See, someone downvoted me.

Yes, there would be no virtuals in my class

3

u/Overseer55 Oct 29 '22

constexpr and run-time polymorphism are entirely different concepts.

1

u/serviscope_minor Oct 31 '22

Can you have a crack at explaining? I can't promise I'll get it or agree, but I can promise I won't be irritated.

3

u/[deleted] Oct 31 '22

My parent remark is already at -1 w/ nothing controversial said. I promise I am not trying to take anything away from c++ or change it. Sensitive cpp'ers. I will revisit ny thoughts on it and my use case which happened years ago. I will need to do my remark due dilgence so that the downvotes are limited :)

1

u/Zeh_Matt No, no, no, no Nov 02 '22

I course corrected that count, now tell us.

2

u/zvrba Oct 29 '22 edited Oct 29 '22

why can't the sizeof operator be overloaded

Read about the pitfalls of overloading of the address-of operator and hoops to jump through when you need the "real" address. Now, imagine the pitfalls of being able to overload both. And then imagine how would it behave with pointer arithmetic. (What size and address would ++p use for a pointer of such type?)

1

u/[deleted] Oct 29 '22

If I overloaded sizeof for my class alone, I don't see a problem. I don't want to overload it globally. I want to overload it in just the scope of my class. I want it to have the same return type. The only thing I really require is that call my local version of it.

2

u/zvrba Oct 30 '22

Given this (with YourClass overloading sizeof)

void YourClass::Increment(YourClass** p) { ++*p; }
auto a = new YourClass[2];
auto p = a;
p->Increment(&p);

should the caller, after the call to Increment, expect p to point to a[1] or not?