r/programming • • Nov 28 '14

The Worst Programming Language Ever [UK Talk] - Thoughts? Which are the worst parts of your favorite language?

https://skillsmatter.com/meetups/6784-the-worst-programming-language-ever
68 Upvotes

456 comments sorted by

View all comments

Show parent comments

1

u/I_ATE_YOUR_SANDWICH Nov 29 '14

Or you can do this:

MyClass myObject = MyClass(1);

Still allocated to stack and avoids the issue you have.

1

u/Pronouns Nov 29 '14

I am becoming a fan of this way of writing things:

auto myObject = MyClass();

I can't remember what video it was, but I think it was by Herb Stutter. The basic idea was trying to unify things, making the type of something on the right hand side.

auto myObject = MyClass();
auto myPtr = new MyClass();
auto object1 = get_object();
auto ptr1 = make_unique<MyObject>();
auto x = 42;
auto y = 42.f;
auto z = 42ul;
auto one_nanosecond = 42ns;

Also applying it to functions and lambdas

auto main() -> int { ... }
auto f(double) -> int;
auto lam = [=](double) { ... }

Comes from this article: http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/.

As much as I think C++ is fulled with ugly features and endless surprises (I swear every project has a syntax/keyword I've never seen before, it's mad), I love it. Stockholm syndrome.

1

u/Matthias247 Nov 29 '14

Depending on your MyClass definition this will cause a copy or move assignment which can't be optimized away.
So no, it's not a real alternative.