c++-standard-library

Does it make sense to combine optional with reference_wrapper?

雨燕双飞 提交于 2020-12-01 01:33:48
问题 It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>> . An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions: Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T* ? Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T* ? 回答1: Is there any

Does it make sense to combine optional with reference_wrapper?

拥有回忆 提交于 2020-12-01 01:29:05
问题 It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>> . An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions: Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T* ? Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T* ? 回答1: Is there any

Will there be a concept for arithmetic types in C++ standard library?

北城以北 提交于 2020-08-06 08:09:46
问题 I've been looking through concepts library on C++ reference and i couldn't find a concept for arithmetic types. I couldn't also find it in p0898. I think such concept would be very helpful. Insted of doing: template <typename T> T some_function(T arg) requires std::integral<T> || std::floating_point<T> { /* functions body */ } I could just do: template <std::arithmetic T> T some_function(T arg) { /* functions body */ } I could obviously define it myself and it wouldn't be hard (ex. template

Will there be a concept for arithmetic types in C++ standard library?

余生长醉 提交于 2020-08-06 08:09:45
问题 I've been looking through concepts library on C++ reference and i couldn't find a concept for arithmetic types. I couldn't also find it in p0898. I think such concept would be very helpful. Insted of doing: template <typename T> T some_function(T arg) requires std::integral<T> || std::floating_point<T> { /* functions body */ } I could just do: template <std::arithmetic T> T some_function(T arg) { /* functions body */ } I could obviously define it myself and it wouldn't be hard (ex. template

make_unique with brace initialization

不羁的心 提交于 2020-06-13 19:08:57
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

make_unique with brace initialization

半世苍凉 提交于 2020-06-13 19:07:33
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

Does any major C++ implementation actually define `NULL` as `nullptr`?

99封情书 提交于 2020-05-15 06:24:08
问题 Since C++11, the Standard allows the macro NULL to either be a integer literal with value zero, or a prvalue of type std::nullptr_t . Any Standard Library vendor deciding to change their definition of NULL from an integer to nullptr would very likely cause breakage for clients relying on pre-C++11 code. Does any major implementation (e.g. GCC, Clang, MSVC) actually define NULL as nullptr ? Or, despite the possibility, does nobody do this? 回答1: libstdc++ relies on including stddef.h, which