c++17

Does std::allocator handle over-aligned types in C++17?

北城以北 提交于 2019-12-04 08:11:38
C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator ? Does it handle over-aligned types? In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2 T* allocate(size_t n); Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T . Compared to C++14, the sentence It is implementation-defined whether over-aligned types are supported has been removed. So std::allocator should support over-aligned types in C++17. 来源: https://stackoverflow.com

`std::variant` vs. inheritance vs. other ways (performance)

那年仲夏 提交于 2019-12-04 07:50:24
问题 I'm wondering about std::variant performance. When should I not use it? It seems like virtual functions are still much better than using std::visit which surprised me! In "A Tour of C++" Bjarne Stroustrup says this about pattern checking after explaining std::holds_alternatives and the overloaded methods: This is basically equivalent to a virtual function call, but potentially faster. As with all claims of performance, this ‘‘potentially faster’’ should be verified by measurements when

What is the correct way to implement iterator and const_iterator in C++17?

元气小坏坏 提交于 2019-12-04 06:15:29
When implementing a custom container I came to the point where I needed to implement iterators. Of course I didn't want to write the code twice for const and non-const iterator. I found this question detailing a possible implementation like this: template<class T> class ContainerIterator { using pointer = T*; using reference = T&; ... }; template<class T> class Container { using iterator_type = ContainerIterator<T>; using const_iterator_type = ContainerIterator<const T>; } But I also found this this question that uses a template parameter: template<class T, bool IsConst> class

cleaning nullptr in one-to-many relation that use custom weak pointer

我们两清 提交于 2019-12-04 05:26:46
I have a one-to-many map class - MyMap1N<WeakPtr_Parent,WeakPtr_Children> . By design, it is supposed to store weak pointers of game-related instance. Roughly speaking, it is called like :- MyMap1N<WeakPtr<Room>,WeakPtr<RigidBody>> map; WeakPtr<Room> room=create<Room>(); WeakPtr<RigidBody> body=create<RigidBody>(); map.add(room,body); MyArray<WeakPtr<RigidBody>> bodys=map.getAllChildren(room); By profiling, I found that std::unordered_map is too slow. Thus, I had to find another way to implement it. I decided to create an array (instead of unordered_map ) in Room . To increase speed of query,

std::launder and strict aliasing rule

好久不见. 提交于 2019-12-04 04:53:05
Consider this code: void f(char * ptr) { auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest // use int_ptr ... } void example_1() { int i = 10; f(reinterpret_cast<char*>(&i)); } void example_2() { alignas(alignof(int)) char storage[sizeof(int)]; new (&storage) int; f(storage); } line of interest with call from example_1: Q1: On the callside the char pointer is aliasing our integer pointer. This is valid. But is it also valid to just cast it back to an int? We know an int is within its lifetime there, but consider the function is defined in another translation unit (with no

Initialize static std::map with non copyable value in a uniformed inline initialization

荒凉一梦 提交于 2019-12-04 04:38:38
I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass . ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not copyable by extending non_copyable that looks like the following: class non_copyable { public: non_copyable() = default; protected: virtual ~non_copyable() = default; private: non_copyable(const non_copyable&) = delete; non_copyable& operator=(const non_copyable&) = delete; }; Now I'm trying to define a std::map using my class as value: static std::map<int, ValueClass> value_classes = { {0,

Understanding Alias Templates

£可爱£侵袭症+ 提交于 2019-12-04 03:51:31
问题 I asked a question that has several references to the code: template <typename...> using void_t = void; I believe I have a generally misunderstand alias templates: Why wouldn't you just evaluate whatever template parameter you're passing into an alias template in an enable_if_t or conditional_t statement? Is the code above just about doing an enable_if_t on multiple template parameters at once? Secondly, I believe that I have a specific misunderstanding of the role of void_t . This comment

Replacing constants: when to use static constexpr and inline constexpr?

旧街凉风 提交于 2019-12-04 03:50:17
问题 This question is a followup question to C++17: still using enums as constants?. Legacy constants come in several forms, notably: #define CONSTANT x enum { CONSTANT = x }; const /*int/unsigned/whatever*/ CONSTANT = x; A comment about static constexpr and inline constexpr constants as a replacement got me thinking on the subject of updating our many, many legacy constants (particularly #define constants). As I understand, an inline constexpr value is basically just substituted in place, like an

Are fold expressions subject to short-circuiting?

送分小仙女□ 提交于 2019-12-04 03:45:22
问题 In C++17, are fold expressions subject to short-circuiting when used with && or || as their operator? If so, where is this specified? 回答1: Yes, fold expressions using && or || as the operator can short-circuit, subject to the usual caveat that it happens for the built-in meaning, but not for an overloaded operator function. The meaning of a fold-expression is defined in [temp.variadic]/9: The instantiation of a fold-expression produces: ((E_1 op E_2) op ... ) op E_N for a unary left fold, E_1

What type will make “std::has_unique_object_representations” return false?

老子叫甜甜 提交于 2019-12-04 03:40:47
At cppref , I see a weird type trait checker : std::has_unique_object_representations From its description, I cannot imagine any type T that make std::has_unique_object_representations<T>::value is false . Is there any counter-example? Understanding the purpose of this trait requires understanding the distinction between an objects "value representation" and its "object representation". From the standard: The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T , where N equals sizeof(T) . The value representation of an object