सी ++ 03 मानक लाइब्रेरी एक क्लास को टाइप करने के दौरान सरल टेम्पलेट प्रकार तर्कों का उपयोग करता है जो आवंटक होने के लिए है। यह संभव है क्योंकि टेम्पलेट्स सी ++ में कैसे काम करते हैं। हालांकि, यह बहुत सरल नहीं है और आपको पता नहीं हो सकता कि वास्तव में किस प्रकार की परिभाषा दिखनी चाहिए - खासकर गैर मानक प्रकारों के मामले में।सी ++ आवंटन प्रकार तर्कों के लिए डिजाइन पैटर्न
मैंने सोचा कि एडाप्टर कक्षाओं को चलाने के लिए यह एक अच्छा विचार हो सकता है। मैं एक उदाहरण बना लिया है आप को दिखाने के लिए मैं क्या मतलब है:
#ifndef HPP_ALLOCATOR_ADAPTOR_INCLUDED
#define HPP_ALLOCATOR_ADAPTOR_INCLUDED
#include <memory>
template<typename T>
struct allocator_traits;
template<typename T, class allocator_type = std::allocator<T>>
class allocator_adaptor;
template<>
struct allocator_traits<void>
{
typedef std::allocator<void>::const_pointer const_pointer;
typedef std::allocator<void>::pointer pointer;
typedef std::allocator<void>::value_type value_type;
};
template<typename T>
struct allocator_traits
{
typedef typename std::allocator<T>::const_pointer const_pointer;
typedef typename std::allocator<T>::const_reference const_reference;
typedef typename std::allocator<T>::difference_type difference_type;
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::reference reference;
typedef typename std::allocator<T>::size_type size_type;
typedef typename std::allocator<T>::value_type value_type;
};
template<class allocator_type>
class allocator_adaptor<void, allocator_type>
: public allocator_traits<void>
{
public:
template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };
};
template<typename T, class allocator_type>
class allocator_adaptor
: public allocator_traits<T>
{
private:
allocator_type m_impl;
public:
template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };
allocator_adaptor() throw() /*noexcept*/;
allocator_adaptor(allocator_adaptor const&) throw() /*noexcept*/;
allocator_adaptor(allocator_type const&) throw() /*noexcept*/;
template<typename U> allocator_adaptor(allocator_adaptor<U, allocator_type> const&) throw() /*noexcept*/;
~allocator_adaptor() throw();
pointer address(reference x) const /*noexcept*/;
const_pointer address(const_reference x) const /*noexcept*/;
pointer allocate (size_type, allocator_traits<void>::const_pointer hint = 0);
void deallocate(pointer p, size_type n) /*noexcept*/;
size_type max_size () const throw() /*noexcept*/;
template<class U, typename... argument_types> void construct(U* p, argument_types&&... args);
template<class U> void destroy(U* p);
};
#endif /* HPP_ALLOCATOR_ADAPTOR_INCLUDED */
अंतर्गत प्रयोग स्पष्ट होना चाहिए। यहां कुछ उपयोग उदाहरण है।
template<class allocator_type>
int max_size(allocator_type const& alloc)
{
// we don't know what kind of max_szie function will be called.
return alloc.max_size();
}
template<typename T>
int max_size(allocator_adaptor<T> const& alloc)
{
// we know what kind of max_size function will be called.
return alloc.max_size();
}
क्या यह सामान्य तरीके की तुलना में एक सुधार है?
मैं कुछ भी पहले लाल पर ऐसा करने के साथ गलत नहीं दिख रहा है तथापि में C++ 0x 'auto' के उपयोग के सबसे एसटीएल उपयोग बहुत सरल बना देता है। – AJG85