Hacker Newsnew | past | comments | ask | show | jobs | submit | sudoankit's commentslogin

For those having the 3rd edition, Q. 3.22, Page 190, however in the textbook unlike the blog post, 1 is if life exists on Mars, 0 if not.


I remember the Mars question from the class I took 20 years ago.


I particularly like Statistical Inference by George Casella and Roger Lee Berger.

You could also look at Introduction to Probability by Joseph K. Blitzstein and Jessica Hwang (available for free here: http://probabilitybook.net (redirects to drive)).


Should be noted that Casella’s book is… well… really great if you thought Spivak’s calculus and Rudin’s analysis to be fun books, especially the exercises.

Casella’s exercises are absolutely brutal.


string product{"not worked"} is initializing the string product to "not worked".

It's the same as std::string product; product = "not worked";

[&](job& my_job) { } is a lambda expression. & is capturing the variable by reference. my_job is the parameter being passed which is a pointer of type job.

Please check https://en.cppreference.com/w/cpp/language/lambda and https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-... for more.


> It's the same as std::string product; product = "not worked";

It's not exactly the same. The original called the converting constructor std::string::string(const char*). Your example calls the std::string default constructor, then the assignment std::string::operator=(const char*). Maybe you didn't mean literally the same, but where trying to illustrate the rough meaning, but the parent commenter said they were familiar with older versions of C++ so I think they'd already be familiar with converting constructors.

It might be more enlightening to say that all of the following are equivalent:

    std::string product{"not worked"};
    std::string product("not worked");
    std::string product = "not worked";
    std::string product = std::string("not worked");
(I'm 90% sure about the last one but can't find documentation for it at the moment.) None of them call the copy constructor std::string::string(const std::string&) or copy assignment operator std::string::operator=(const std::string&), although in older versions of the C++ standard the last two required that the relevant assignment operators (std::string::operator=(const char*) and std::string::operator=(const std::string&) respectively) to be accessible even though it wasn't called.

For other combinations of types, these different syntaxes are not equivalent. For example, uniform initialisation (with the braces) won't allow narrowing conversions, such as short to int or double to float.


The last one did copy in the olden days, but now with copy elision it does not. You can verify this using -fno-elide-constructors


You're right, I was being sloppy and gave a rough example. My bad.


> It's the same as std::string product; product = "not worked";

Not a C++ person, so please forgive my ignorance, but what is the difference between the above and sth. like:

    std::string product = "not worked";
or

    std::string product("not worked");
(Are these even legal C++ statements and, if not, why not? ;-))


Both are valid, and were the only ways of initializing objects before C++11, I think.

Brace initialization has an advantage in that it allows you to initialize compound values, like containers and structs, even if they're nested:

    std::map<int, std::string> m = { // nested list-initialization
           {1, "a"},
           {2, {'a', 'b', 'c'} },
           {3, s1}
    };
 
Ref: https://en.cppreference.com/w/cpp/language/list_initializati... https://en.cppreference.com/w/cpp/language/aggregate_initial...


it's also generally preferred because it can avoid certain narrowing conversions in construction: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines...


How compile-time expensive is this? I guess it has to potentially consider a lot of constructor combinations?


I see, that's interesting.


In practical terms, none. They are the same, and the same as using the curly braces.

From the POV of the standard, those are different kinds of initializations. C++ has like 12 different ways of initializing variables, and the differences are quite confusing, but in practical terms in my experience I never had to care too much beside making sure native types are initialized to some specified value.

You can probably find some talks on YouTube talking about the initializations of c++, and 1h30m is probably not enought to cover all the details :')


Yeah, that sounds like C++, I suppose. Thanks very much.


Thanks very much for the explanations, folks! One more follow-up question: what are reasons to prefer:

     std::string product; 
     product = "not worked";
i.e., separate declaration and initialization, as suggested by the grand-parent?


For this particular example direct initialization i.e. std::string product("not worked"); would be preferred, as you end up using one call to constructor instead of two: default constructor followed by the move assignment.

https://en.cppreference.com/w/cpp/language/direct_initializa... https://en.cppreference.com/w/cpp/language/move_assignment


Thank you very much.

What are examples of situations where the other pattern would be preferable?


afaik the only time you are really supposed to use uninitialized values is when you are planning on reading it in from some stream.

otherwise you should really avoid it.


Both are legal statements.

The second one is a direct initialization, invoking corresponding constructor.

The first one first invokes default constructor and then copy or move assignment depending on rvalueness of the arg.


Exactly. Safari on MacOS has the same problem too.


Same but instead of Air I’ll get the 13” ARM Pro with sufficient RAM and hopefully more ports.

Mojave on my 13” MBP 2015 is still fantastic. Great OS, great keyboard, USB ports, MagSafe, etc.


Same RAM, same ports.


Yep. Seems like the only difference is GPU cores.


Also better thermal management.


Incredible!


Agreed

But I don't even know enough to know how incredible it is

I studied Biology before but had issue remembering the names


I made this today for folks who would love to learn a bit more about Lagrangian Multipliers. Please criticize and comment!


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: