Boost :: Concept

zhaozj2021-02-16  33

Sender: huxw (Mi Duck and Tang Mouse), the letter area: Programming Title: Boost :: Concept_Check Send Station: BBS Shuimu Tsinghua Station (Mon May 13 14:21:45 2002)

Follow up, this is relatively simple;)

Boost concept check library

One: Why?

"Concept Check" means similar to Design by Contract, I think. A simple example,

1 #include 2 #include 3 4 INT Main (int, char * []) ​​{5 std :: list v; 6 std :: stable_sort (v.begin (), v.end ()); 7 returnography; 8}

This has an obvious mistake. List does not have a random access, and it is obviously not to meet the requirements of Stable_Sort. That is, this program is wrong. But can the compiler tell us? See what they are saying .

[VC6]: / Program Files / Microsoft VisualStudio / VC98 / include / algorithm (579): Error C2784: 'Classstd :: Reverse_Iterator <_ri, _ty, _rt, _pt, _d> __cdecl std :: operator (_ D, const class std :: REVERSE_ITERATOR <_ri, _ty, _rt, _pt, _d> &): COULDNOT DEDUCE TEMPLATE ARG Ument for '' from 'Class std :: list > :: item' C: / program files / microsoft visualstudio / vc98 / include / algorithm (570): see reference to functiontemplate instantiation 'void __cdecl std :: _ Insertion_sort_1 (classstd :: list > :: iterator, classstd :: List > :: item, int *) 'Beingcompiled C: / Program Files / Microsoft Visualstudio / VC98 / include / algorithm (579): Error C2676: Binary' ': 'classstd :: list > :: iterator' does not definethis operator or a conversion to a type acceptable to the predefinedoperato rc: / program files / microsoft visualstudio / vc98 / include / algorithm ( 570): See Reference To FunctionTemplate Instant, 'Void __cdecl ST D :: _ Insertion_Sort_1 (Classstd :: List > :: itrator, classstd :: list > :: iterator, int *) 'beingcompiled [G 2.95.3] D: / mingw / bin /../2 / gcc-lib / mingw32 / 2.95.3-5 /../..../../ include / g - 3 / STL_ALGOH: In Function `Void __inplace_stable_sort <_list_iterator > (_L .... This is about 50 rows H: 1489: No match for` _List_iterator _List_iterator & '

For an initiator, the error message given by the compiler not only does not illustrate the reason, but also intentionally, the idea of ​​people goes into the wrong direction. The author of Concept_Check summarizes this four points:

1. Because the compiler error caused by the wrong template parameter is similar. It is often erroneous from the caller of the template, but the compiler throws out the internal implementation of the template to see .2. Document as a document The CONCEPT may not completely match the template itself, so it may appear, the user is clearly called the template in the documentation, or there is an inexplicable error

3. Documentized Concept is too bound to exceed the template truly needs.

4. Concept is not expressed in the code, which makes the code difficult to understand; and the code and document are difficult to synchronize.

Compiled

[G 2.95.3] D: / mingw / bin /../2 / gcc-lib / mingw32 / 2.95.3-5 /../..../../ include / g - 3 / STL_ALGOH: In Function `Void __inplace_stable_sort <_list_iterator > (_L .... This is about 50 rows H: 1489: No match for` _List_iterator _List_iterator & '

For an initiator, the error message given by the compiler not only does not illustrate the reason, but also intentionally, the idea of ​​people goes into the wrong direction. The author of Concept_Check summarizes this four points:

1. Because the compiler error caused by the wrong template parameters is similar. It is often erroneous in the template caller, but the compiler throws out the internal implementation of the template to see the programmer.

2. The Concept recorded as a document may not be completely consistent with the template itself, so it is possible that the user calls the template in the instructions of the document, or there is an inexplicable error.

3. Documentized Concept is too bound to exceed the template truly needs.

4. Concept is not expressed in the code, which makes the code difficult to understand; and the code and document are difficult to synchronize.

Concept_Check can provide:

1. Insert the compile period template parameter check

2. Create a framework that expresses the Concept request by the Concept Checking class (Framework)

3. Mechanism that can verify template parameters

II. How to use

Concept is very simple, two things [function_requires] is used inside the template function. such as Template void generic_library_function (t x) {function_requires > (); // ... }; [Boost_class_requires] is used inside a template class. For example, Template struct generic_library_class {boost_class_requires (t, equalitycomparableconcept); // ...};

In a just example, if we have modified the Stable_Sort code in Algorithm, then ...

The result of the output should be sauce purple:

D: / mingw / bin / mingw32 / 2.95.3-5 /../..../../ incrude / boost / concept_check.hpp: in Method `Void boost: : ComparableConcept <_list_iterator > :: constraints () (): d: / mingw / bin /../2.95.3-5 /../ .. /../../include/boost/concept_check.hpp:43: Instantiated from `Boost :: function_requires >> () 'd: / mingw / BIN / MINGW32 / 2.95.3-5 / MingW32 / 2.95.3-5 /../../../../ incrude / boost / concept_check.hpp: 607: instantiated from `Boost :: randomaccessITeratorConcept <_list_iterator > :: constraints () 'd: / mingw / bin /../2/gcc-lib / mingw32 / 2.95.3-5 /../../../. ./include/boost/concept_check.hpp:43: Instantiated from `Boost :: function_requires >> () 'd: / mingw / bin /../ LIB / GCC-LIB / MINGW32 / 2.95.3-5 /../../../ include / g - 3 / STL_ALGO.H: 1544: Instantiated from `Stable_sort <_list_iterator > (_ list_iterator , _list_iterator ) 'QQ.cpp: 6: Instantiated from hered: / mingw / bin / mingW32 / 2.95.3-5 /../../../. ./include/boost/concept_check.hpp:282: No match for `_List_iterator & <_list_iterator & '... I have seen at least here. RandomaccessITeratorConcept, QQ.cpp: 6 This keyword. And the word "of Concept_Check is also reminding the programmer, the reason for the error occurred in yourself rather than the library. The error message is still very long, although it is a bit more than the original. ; (

3. Implementation

The CONCEPT class implementation is simple, there must be a non-static member function for void constraints (), which is used back to function_requires. And, it is best to add a concess suffix to your class name, which can explain This class is dry.

Other

It is best to look at the concept_check.hpp file, the code is not complicated, boost_class_requires appear here to the keywords such as RandomaccessIitorConcept, QQ.cpp: 6. And the word "of Concept_Check is also reminding the programmer, the cause of the error is in yourself Not the library. The error message is still very long, although it is a little bit more than the original. (3. Implemented in the expansion

The CONCEPT class implementation is simple, there must be a non-static member function for void constraints (), which is used back to function_requires. And, it is best to add a concess suffix to your class name, which can explain This class is dry.

Other

It is best to look at the concept_check.hpp file, the code is not complicated, boost_class_requires is a macro for assembling the function.

- AbcdefghijklmnopqrStuvwxyzabcdefghijklmnopqrStuvwxyz

※ Source: · BBS Shuimu Tsinghua Station bbs.edu.cn · [from: 166.111.64.166]

转载请注明原文地址:https://www.9cbs.com/read-27788.html

New Post(0)