Extension of Iterator Traits in The C++ Standard Template Library
Extension of Iterator Traits in The C++ Standard Template Library
AN PORKOL
AB: EXTENSION OF ITERATOR TRAITS IN THE C++ STANDARD TEMPLATE LIBRARY 913
COPY_ALGORITHM_WITHOUT_\
INSERTER_ITERATOR()
);
return copy( first,
last,
result,
__inserting_iterator_tag() );
}
VI. BELIEVE-ME MARKS
Generally, warnings should be eliminated. On the other
hand, the call of copy or transform without inserter
iterators does not mean problem necessarily.
If the proposed extensions are in use, the following code
snippet results in a warning message, but it works perfectly:
std::vector<int> vi;
// ...
std::list<int> li( vi.size() );
std::copy( vi.begin(),
vi.end(),
li.begin() );
Many similar patterns can be shown. We use copy or
transform algorithm to a target, where enough allocated
space is available. Moreover, we cannot disable these specic
generated warnings by a compiler ag or a preprocessor
pragma.
Believe-me marks [9] are used to identify the points in the
programtext where the type system cannot obtain if the used
construct is risky. For instance, in the hereinafter example,
the user of the library asks the type system to believe that
the target is already allocated in the proper way. This way
we enforce the user to reason about the parameters of these
algorithms.
std::vector<int> v;
// ...
std::list<int> li( v.size() );
std::copy( v.begin(),
v.end(),
li.begin(),
transmogrify,
ALREADY_ALLOCATED );
This can be created by a preprocessor macro:
#define ALREADY_ALLOCATED \
__inserting_iterator_tag()
VII. CONCLUSION
STL is the most widely-used library based on the generic
programming paradigm. It is efcient and convenient, but the
incorrect usage of the library results in weird or undened
behaviour.
In this paper we present some examples that can be com-
piled, but at runtime their usage is defective. We argue for an
extension of the iterator traits in the library, and based on this
extension we generate warning messages during compilation.
The effect of our approach is similar to the STLlint software.
STLlint analyzes the programtext and emits warning messages
when the STL is used in an erronous way. STLlint is based on
a modied compiler and this way it can emit better messages.
On the other hand, it is not extensible. Our approach can be
used for non-standard containers, iterators, algorithms, too.
Compilers cannot know all the generic libraries.
We present an effective approach to generate custom warn-
ings. Believe-me marks are also written to disable warning
messages. We overload some algorithms of the STL based on
the new traits in order to make the usage of the library safer.
ACKNOWLEDGMENT
The Project is supported by the European Union and co-
nanced by the European Social Fund (grant agreement no.
T
AMOP 4.2.1./B-09/1/KMR-2010-0003).
REFERENCES
[1] A. Alexandrescu, Modern C++ Design, Addison-Wesley, 2001.
[2] M. H. Austern, Generic Programming and the STL: Using and Extending
the C++ Standard Template Library, Addison-Wesley, 1998.
[3] T. Becker, STL & generic programming: writing your own iterators,
C/C++ Users Journal 2001 19(8), pp. 5157.
[4] G. D evai, N. Pataki, Towards veried usage of the C++ Standard
Template Library, In Proc. of the 10th Symposium on Programming
Languages and Software Tools (SPLST) 2007, pp. 360371.
[5] G. D evai, N. Pataki, A tool for formally specifying the C++ Standard
Template Library, In Annales Universitatis Scientiarum Budapestinensis
de Rolando E otv os Nominatae, Sectio Computatorica 31, pp. 147166.
[6] D. Gregor, J. J arvi, J. Siek, B. Stroustrup, G. Dos Reis, A. Lumsdaine,
Concepts: linguistic support for generic programming in C++, in Proc.
of the 21st annual ACM SIGPLAN conference on Object-oriented
programming systems, languages, and applications (OOPSLA 2006), pp.
291310.
[7] D. Gregor, S. Schupp, Stllint: lifting static checking from languages to
libraries, Software - Practice & Experience, 2006 36(3), pp. 225-254.
[8] J. J arvi, D. Gregor, J. Willcock, A. Lumsdaine, J. Siek, Algorithm
specialization in generic programming: challenges of constrained gener-
ics in C++, in Proc. of the 2006 ACM SIGPLAN conference on
Programming language design and implementation (PLDI 2006), pp.
272282.
[9] T. Kozsik, T., Tutorial on Subtype Marks, in Proc. of the Central
European Functional Programming School (CEFP 2006), LNCS 4164,
pp. 191222.
[10] S. Meyers, Effective STL - 50 Specic Ways to Improve Your Use of the
Standard Template Library, Addison-Wesley, 2001.
[11] N. Pataki, Advanced Functor Framework for C++ Standard Template
Library Studia Universitatis Babes-Bolyai, Informatica, Vol. LVI(1), pp.
99113.
[12] N. Pataki, Z. Porkol ab, Z. Istenes, Towards Soundness Examination of
the C++ Standard Template Library, In Proc. of Electronic Computers
and Informatics, ECI 2006, pp. 186191.
[13] N. Pataki, Z. Sz ugyi, G. D evai, Measuring the Overhead of C++ Stan-
dard Template Library Safe Variants, In Electronic Notes in Theoretical
Computer Science (ENTCS) 264(5), pp. 7183.
[14] Z. Porkol ab,
A. Sipos, N. Pataki, Inconsistencies of Metrics in C++
Standard Template Library, In Proc. of 11th ECOOP Workshop
on Quantitative Approaches in Object-Oriented Software Engineering
QAOOSE Workshop, ECOOP 2007, Berlin, pp. 26.
[15] B. Stroustrup, The C++ Programming Language (Special Edition),
Addison-Wesley, 2000.
[16] M. Torgersen, The Expression Problem Revisited Four New Solutions
Using Generics, in Proc. of European Conference on Object-Oriented
Programming (ECOOP) 2004, LNCS 3086, pp. 123143.
[17] L. Zolman, An STL message decryptor for visual C++, In C/C++ Users
Journal, 2001 19(7), pp. 2430.
[18] I. Z olyomi, Z. Porkol ab, Towards a General Template Introspection Li-
brary, in Proc. of Generative Programming and Component Engineering:
Third International Conference (GPCE 2004), LNCS 3286, pp. 266282.
914 PROCEEDINGS OF THE FEDCSIS. SZCZECIN, 2011