Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

###alloc.hpp

alloc.hpp

###alloc.hpp

alloc.hpp

Spelling, grammar etc.
Source Link
Toby Speight
  • 87.5k
  • 14
  • 104
  • 323

I attempt to write a STL-like vector, mostly becauseto figure out how is it works. I wonder which parts are lookslook weird or what I made stupid. Any kind of comment is appreciated:

alloc###alloc.hpp

vector.hpp

vector.hpp

I attempt to write a STL-like vector, mostly because figure out how is it works. I wonder which parts are looks weird or what I made stupid. Any kind of comment is appreciated:

alloc.hpp

vector.hpp

I attempt to write a STL-like vector, mostly to figure out how it works. I wonder which parts look weird or what I made stupid. Any kind of comment is appreciated:

###alloc.hpp

vector.hpp

Posted alloc.hpp
Source Link
user201793
user201793

alloc.hpp

#pragma once
#include <limits>
#include <memory>
namespace p1v0t {
template <class T>
class allocator {
public:
 using value_type = T;
 template <class U>
 struct rebind {
 typedef allocator<U> other;
 };
 allocator() noexcept = default;
 template <class U>
 allocator(allocator<U> const &) noexcept {}
 // Use pointer if pointer is not a value_type*
 [[nodiscard]] value_type *allocate(std::size_t n) {
 return static_cast<value_type *>(::operator new(n * sizeof(value_type)));
 }
 // Use pointer if pointer is not a value_type*
 void deallocate(value_type *p, std::size_t) noexcept { ::operator delete(p); }
 using propagate_on_container_copy_assignment = std::false_type;
 using propagate_on_container_move_assignment = std::false_type;
 using propagate_on_container_swap = std::false_type;
 using is_always_equal = std::is_empty<allocator>;
};
template <class T, class U>
bool operator==(allocator<T> const &, allocator<U> const &) noexcept {
 return true;
}
template <class T, class U>
bool operator!=(allocator<T> const &x, allocator<U> const &y) noexcept {
 return !(x == y);
}
} // namespace p1v0t

vector.hpp

alloc.hpp

#pragma once
#include <limits>
#include <memory>
namespace p1v0t {
template <class T>
class allocator {
public:
 using value_type = T;
 template <class U>
 struct rebind {
 typedef allocator<U> other;
 };
 allocator() noexcept = default;
 template <class U>
 allocator(allocator<U> const &) noexcept {}
 // Use pointer if pointer is not a value_type*
 [[nodiscard]] value_type *allocate(std::size_t n) {
 return static_cast<value_type *>(::operator new(n * sizeof(value_type)));
 }
 // Use pointer if pointer is not a value_type*
 void deallocate(value_type *p, std::size_t) noexcept { ::operator delete(p); }
 using propagate_on_container_copy_assignment = std::false_type;
 using propagate_on_container_move_assignment = std::false_type;
 using propagate_on_container_swap = std::false_type;
 using is_always_equal = std::is_empty<allocator>;
};
template <class T, class U>
bool operator==(allocator<T> const &, allocator<U> const &) noexcept {
 return true;
}
template <class T, class U>
bool operator!=(allocator<T> const &x, allocator<U> const &y) noexcept {
 return !(x == y);
}
} // namespace p1v0t

vector.hpp

Tweeted twitter.com/StackCodeReview/status/1149151685903835137
Source Link
user201793
user201793
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /