Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   The STL is really neat (http://www.programmingforums.org/showthread.php?t=13818)

Jessehk Aug 20th, 2007 7:19 PM

The STL is really neat
 
http://www.sgi.com/tech/stl/table_of_contents.html

I don't know why I never realized it before, but the STL is really, really neat. The STL idea of concepts combined with boost::concept_traits allows you to check that template parameters pass certain requirements.

Example:
:

#include <iostream>
#include <vector>

#include <boost/concept_check.hpp>


template <typename InputIterator>
void display( InputIterator begin, InputIterator end ) {
    using namespace boost;
   
    function_requires<InputIteratorConcept<InputIterator> >();
   
    for (; begin != end; begin++ )
        std::cout << *begin;
   
    std::cout << std::endl;
}

int main() {
    std::vector<int> numbs;
   
    for ( int x = 0; x < 10; x++ )
        numbs.push_back( x );
   
    display( numbs.begin(), numbs.end() );
}


If I've done that right, it will only compile if the type of iterator passed to the display() function complies by the Input Iterator concept defined by the STL.

Here's another example:
:

template <typename Integer>
bool isEven( Integer n ) {
    using namespace boost;
   
    function_requires<IntegerConcept<Integer> >();
   
    return !(n & 1);
}

int main() {
    std::cout << isEven( 314 ) << std::endl; // Is fine --> concept check passes
    std::cout << isEven( 4.2 ) << std::endl; // BAD --> compile error
}


C++ continues to astound me with what it's capable of. :eek:

Cache Aug 21st, 2007 5:44 AM

Quote:

Originally Posted by Jessehk (Post 132561)
C++ continues to astound me with what it's capable of. :eek:

Then hold your pants on:
http://www.open-std.org/jtc1/sc22/wg...2005/n1758.pdf


All times are GMT -5. The time now is 3:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC