MESSAGE
DATE | 2016-11-07 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] templates and ostream for future reference
|
From learn-bounces-at-nylxs.com Mon Nov 7 19:56:06 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id E0369161312; Mon, 7 Nov 2016 19:55:21 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 36A8A160E77 for ; Mon, 7 Nov 2016 19:55:19 -0500 (EST) To: learn-at-nylxs.com From: Ruben Safir Message-ID: <55b9fa4c-8051-1596-d43a-e83a0d09d8ab-at-mrbrklyn.com> Date: Mon, 7 Nov 2016 19:55:19 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Subject: [Learn] templates and ostream for future reference X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
http://cppannotations.sourceforge.net/annotations/html/cplusplus23.html
23.4.1: Policy classes - I A policy defines (in some contexts: prescribes) a particular kind of behavior. In C++ a policy class defines a certain part of the class interface. It may also define inner types, member functions, and data members.
In the previous section the problem of creating a class that might use any of a series of allocation schemes was introduced. These allocation schemes all depend on the actual data type to use, and so the `template reflex' should kick in.
Allocation schemes should probably be defined as template classes, applying the appropriate allocation procedures to the data type at hand. When such allocation schemes are used by the familiar STL containers (like std::vector, std::stack, etc.), then such home-made allocation schemes should probably be derived from std::allocator, to provide for the requirements made by these containers. The class template std::allocator is declared by the header file and the three allocation schemes developed here were all derived from std::allocator.
Using in-class implementations for brevity the following allocation classes could be defined:
No special allocation takes place, Data is used `as is':
template class PlainAlloc: public std::allocator { template friend std::ostream &operator<<(std::ostream &out, PlainAlloc const &alloc); Data d_data;
public: PlainAlloc() {} PlainAlloc(Data const &data) : d_data(data) {} PlainAlloc(PlainAlloc const &other) : d_data(other.d_data) {} };
The second allocation scheme uses the standard new operator to allocate a new copy of the data:
template class NewAlloc: public std::allocator { template friend std::ostream &operator<<(std::ostream &out, NewAlloc const &alloc); Data *d_data;
public: NewAlloc() : d_data(0) {} NewAlloc(Data const &data) : d_data(new Data(data)) {} NewAlloc(NewAlloc const &other) : d_data(new Data(*other.d_data)) {} ~NewAlloc() { delete d_data; } };
The third allocation scheme uses the placement new operator (see section 9.1.5), requesting memory from a common pool (the implementation of the member request, obtaining the required amount of memory, is left as an exercise to the reader):
template class PlacementAlloc: public std::allocator { template friend std::ostream &operator<<(std::ostream &out, PlacementAlloc const &alloc); Data *d_data;
static char s_commonPool[]; static char *s_free;
public: PlacementAlloc() : d_data(0) {} PlacementAlloc(Data const &data) : d_data(new(request()) Data(data)) {} PlacementAlloc(PlacementAlloc const &other) : d_data(new(request()) Data(*other.d_data)) {} ~PlacementAlloc() { d_data->~Data(); } private: static char *request(); };
The above three classes define policies that may be selected by the user of the class Storage introduced in the previous section. In addition to these classes, additional allocation schemes could be implemented by the user as well.
To apply the proper allocation scheme to the class Storage, Storage should be designed as a class template itself. The class also needs a template type parameter allowing users to specify the data type.
The data type to be used by a particular allocation scheme could of course be specified when specifying the allocation scheme to use. The class Storage would then have two template type parameters, one for the data type, one for the allocation scheme:
template class Storage ...
To use the class Storage we would then write, e.g.:
Storage> storage;
Using Storage this way is fairly complex and potentially error-prone, as it requires the user to specify the data type twice. Instead, the allocation scheme should be specified using a new type of template parameter, not requiring the user to specify the data type required by the allocation scheme. This new kind of template parameter (in addition to the well-known template type parameter and template non-type parameter) is called the template template parameter.
Starting with the C++14 standard using the keyword class in the syntactical form of template template parameters (template specifications> class Name) is no longer required. From that standard onward, the keyword typename can also be used (e.g., template specifications> typename Name).
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 _______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|