c++17

Type trait for aggregate initializability in the standard library?

守給你的承諾、 提交于 2021-02-18 22:25:34
问题 The C++ standard library has std::is_constructible<Class, T...> to check if a class can be constructed from the given types as arguments. For example, if I have a class MyClass which has a constructor MyClass(int, char) , then std::is_constructible<MyClass, int, char>::value will be true . Is there a similar standard library type trait that will check that aggregate initialization works, i.e. MyClass{int, char} is well-formed and returns a MyClass ? My use case: I want to write a function

How to invoke aligned new/delete properly?

北城以北 提交于 2021-02-18 21:58:27
问题 How do I call new operator with alignment? auto foo = new(std::align_val_t(32)) Foo; //? and then, how to delete it properly? delete(std::align_val_t(32), foo); //? If this is the right form of using these overloads, why valgring complaining about mismatched free()/delete/delete[]? 回答1: exist very basic principle - the memory free routine always must match to allocate routine. if we use mismatch allocate and free - run time behavior can be any: all can be random ok, or crash by run-time, or

How to invoke aligned new/delete properly?

允我心安 提交于 2021-02-18 21:53:08
问题 How do I call new operator with alignment? auto foo = new(std::align_val_t(32)) Foo; //? and then, how to delete it properly? delete(std::align_val_t(32), foo); //? If this is the right form of using these overloads, why valgring complaining about mismatched free()/delete/delete[]? 回答1: exist very basic principle - the memory free routine always must match to allocate routine. if we use mismatch allocate and free - run time behavior can be any: all can be random ok, or crash by run-time, or

Providing tuple-like structured binding access for a class

安稳与你 提交于 2021-02-18 21:12:24
问题 I'm trying to support tuple-like structured binding access for a class. For simplicity, I'll use the following class in the rest of this post: struct Test { int v = 42; }; (I'm aware that this class supports structured bindings out of the box but let's assume it does not.) To enable tuple-like access to the member of Test , we must specialize std::tuple_size and std::tuple_element : namespace std { template<> struct tuple_size<Test> { static const std::size_t value = 1; }; template<std::size

Best way to create a setter function in C++

橙三吉。 提交于 2021-02-18 12:32:05
问题 I want to write a template function that receives parameter by move or by copy. The most efficient way that I use is: void setA(A a) { m_a = std::move(a); } Here, when we use is A a; setA(a); // <<---- one copy ctor & one move ctor setA(std::move(a)); // <<---- two move ctors I recently found out that defining it this way, with two functions: void setA(A&& a) { m_a = std::move(a); } void setA(const A& a) { m_a = a; // of course we can so "m_a = std::move(a);" too, since it will do nothing }

Qt: construct a mutable iterator for template (maps, lists, sets, …)

人盡茶涼 提交于 2021-02-18 11:29:45
问题 In my code I often have functions doing the same thing on different iterable Qt Container types, for instance: void removeX(QMap<qint64, QString> & map) { QMutableMapIterator<qint64, QString> it(map); while (it.hasNext()) { it.next(); if (it.value() == "X") it.remove(); } } void removeX(QList<QString> & list) { QMutableListIterator<QString> it(list); while (it.hasNext()) { it.next(); if (it.value() == "X") it.remove(); } } (and I know there is already a removeAll function in QList. This is

Overloading operator new with smaller default alignment

左心房为你撑大大i 提交于 2021-02-18 11:18:05
问题 C++17 introduced Dynamic memory allocation for over-aligned data Beside the existing std::max_align_t , the fundamental alignment, it added __STDCPP_DEFAULT_NEW_ALIGNMENT__ the minimal alignment that the operator new guarantees. With MSVC2017 64bit compilation, these constants result in a std::max_align_t of size 8 and __STDCPP_DEFAULT_NEW_ALIGNMENT__ of size 16. It is however allowed to overrule the operator new/free, as mentioned on cppreference: operator new - global replacements. Looking

If there's an if-constexpr, how come there's no switch-constexpr?

时光怂恿深爱的人放手 提交于 2021-02-18 11:17:04
问题 In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr , is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)? 回答1: if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have

How to check if type is explicitly/implicitly constructible?

喜夏-厌秋 提交于 2021-02-18 06:25:10
问题 How can it be checked that some type is explicitly (or vice versa implicitly) constructible from other type? Is it any SFINAE trick in this situation? I can write is_explicitly_constructible as a combination of std::is_constructible and std::is_convertible: #include <type_traits> template <typename Type, typename Argument> struct is_explicitly_constructible : std::bool_constant < std::is_constructible<Type, Argument>::value && !std::is_convertible<Argument, Type>::value > { }; But do I take

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