rationale

Why introduce `std::launder` rather than have the compiler take care of it?

孤人 提交于 2021-02-17 22:01:00
问题 I've just read What is the purpose of std::launder? and frankly, I am left scratching my head. Let's start with the second example in @NicolBolas' accepted answer: aligned_storage<sizeof(int), alignof(int)>::type data; new(&data) int; int *p = std::launder(reinterpret_cast<int*>(&data)); [basic.life]/8 tells us that, if you allocate a new object in the storage of the old one, you cannot access the new object through pointers to the old. std::launder allows us to side-step that. So, why not

Empty struct in C vs empty struct in C++

风流意气都作罢 提交于 2021-02-07 02:49:29
问题 Why is empty struct in C a constraint violation? Why does this rule get changed in C++? Are there any historical reasons? 回答1: since you don't have inheritance in C you don't need them. If you just want to have a distinguishable pointer type you can use pointers to incomplete types. struct opaque; struct opaque* stranger = 0; should work fine. 回答2: My guess is this: In C, there isn't inheritance, templates, and function overloading - three major reasons we use empty structs in C++ - as a base

What do we need std::as_const() for?

你离开我真会死。 提交于 2020-12-29 08:52:30
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

What do we need std::as_const() for?

让人想犯罪 __ 提交于 2020-12-29 08:52:23
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

What do we need std::as_const() for?

情到浓时终转凉″ 提交于 2020-12-29 08:52:07
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

What do we need std::as_const() for?

吃可爱长大的小学妹 提交于 2020-12-29 08:52:01
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

Why was std::swap moved to <utility>?

流过昼夜 提交于 2020-04-08 08:47:12
问题 Why has std::swap been moved to the <utility> header for C++11? N3290 C.2.7 says: 17.6.3.2 Effect on original feature: Function swap moved to a different header Rationale: Remove dependency on <algorithm> for swap. Effect on original feature: Valid C++ 2003 code that has been compiled expecting swap to be in <algorithm> may have to instead include <utility> . I can't understand the part in bold. What kind of dependency is being talked about and why? 回答1: The committee wanted to allow you to

Why was std::swap moved to <utility>?

妖精的绣舞 提交于 2020-04-08 08:44:45
问题 Why has std::swap been moved to the <utility> header for C++11? N3290 C.2.7 says: 17.6.3.2 Effect on original feature: Function swap moved to a different header Rationale: Remove dependency on <algorithm> for swap. Effect on original feature: Valid C++ 2003 code that has been compiled expecting swap to be in <algorithm> may have to instead include <utility> . I can't understand the part in bold. What kind of dependency is being talked about and why? 回答1: The committee wanted to allow you to

Why is AngularJS considered MV*

时光毁灭记忆、已成空白 提交于 2019-12-23 04:57:35
问题 I have worked with MVC on the back-end (Rails), and am currently working with MVC(MV*) on the front-end (Angular). I have seen Angular as considered an MV* pattern, but why is it considered that exactly? Using Angular, I understand the separation of concerns, with Views (templates), Controllers, and use Services to serve up data. In this case, the model (data store) via ng-model makes sense for front-end temporary storage, but the actual persistence (when a POST or PUT is made to an API)

Why is it ill-formed to have multi-line constexpr functions?

走远了吗. 提交于 2019-12-21 06:48:52
问题 According to Generalized Constant Expressions—Revision 5 the following is illegal. constexpr int g(int n) // error: body not just ‘‘return expr’’ { int r = n; while (--n > 1) r *= n; return r; } This is because all 'constexpr' functions are required to be of the form { return expression; } . I can't see any reason that this needs to be so. In my mind, the only thing that would really be necessary is that no external state information is read/written and the parameters being passed in are also