Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 20th, 2007, 6:19 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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:
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Aug 21st, 2007, 4:44 AM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Jessehk View Post
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
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 neat games JDStud6 Coder's Corner Lounge 45 Jan 3rd, 2006 5:57 PM
Neat little program... Sane Python 3 Apr 10th, 2005 5:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:32 AM.

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