Category : traits

I have come across the interesting and powerful concept of "traits" recently and am attempting to understand/implement them in C++. From what I understand, traits provide a way both extend/adapt the functionality of exiting code and define an "interface" for a class without using traditional inheritance (and all the overhead/problems that come with it). I ..

Read more

Is it possible to specialize the std::allocator_traits, template like this? namespace falloc_ { template<class Tp> class FAllocator ; } // partial spec for all falloc_::FAllocator<U>, std::allocator_traits template<typename Tp> struct std::allocator_traits<falloc_::FAllocator<Tp> > { using allocator_type = falloc_::FAllocator<Tp> ; using value_type = typename allocator_type::value_type ; // … // All the components I need here, I will include ..

Read more

I am trying to implement very basic shared_ptr (most functions skipped in question for simplicity), and for the case we have a pointer of base and create a derived pointer I created a separate constructor. template<typename T> class shared_ptr { public: shared_ptr(T* resource) :pointee_(resource), refCount_(new size_t(1)) { std::cout << typeid(resource).name(); deleter_ = [=]() {delete resource; ..

Read more

I am trying to find a way to group the main types and constants used by my project into a namespace, and then I’d like to import them in all my classes with "using namespace". I can’t figure out why this piece of code doesn’t compile, g++ error says: expected nested-name-specifier before ‘namespace’ What options ..

Read more

I trying to implement std::list (MSVC). And one thing I cannot understand: template <class _Value_type, class _Voidptr> // voidptr? For what? struct _List_node { // list node using value_type = _Value_type; using _Nodeptr = _Rebind_pointer_t<_Voidptr, _List_node>; // what is the purpose of such rebind? … } I understand the reason of allocator rebind, but pointer? ..

Read more

Consider a typical (simplified) implementation of std::advance function for iterators: template<typename Itarator,typename Dist> void advance(Iterator& it, Dist n) { typedef std::itarator_traits<Itarator>::itarator_category c; advance_impl(it, n, c); } template<typename Iterator, typename Dist> void advance_impl(Itarator& it, Dist n, std::random_access_itaerator_tag) { it += n; } template<typename Iterator, typename Dist> void advance_impl(Itarator& it, Dist n, std::input_iterator_tag) { while (n–)++it; } ..

Read more