![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Then hold your pants on:
http://www.open-std.org/jtc1/sc22/wg...2005/n1758.pdf |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |