Using C++ and ctypes together: a vast conspiracy? ;)
A. Cavallo
a.cavallo at mailsnare.com
Wed Jun 3 06:10:38 EDT 2009
> >> > No wonder, you have never actually used C++ with C types. An extern
> >> > "C" clause tells the compiler to generate C functions (more precisely,
> >> > functions that conform to the C ABI conventions), so effectively
> >> > you're calling into C, not into C++.
> >>
> >> Seems like the only sane way to do it. In all other directions lies
> >> madness.
> >
> > Yes but creating C stubs is also hard in presence of everything that
> > is not basic C++. How would you wrap the STL?
>
> What does the STL offer that Python doesn't already do more flexibly and
> more simply?
Couldn't agree more:)
The following is the STL equivalent of:
print [ x*2 for range(10) in data if (x%2 == 0) ]
It will take more than digging into the STL documentation.....
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
#include <ext/functional>
using __gnu_cxx::compose1;
class Range
{
public:
Range(int start=0) : m_start(start) {}
virtual ~Range() {}
int operator()() { return m_start++; }
private:
int m_start;
};
class Times
{
public:
Times(int val) : m_val(val) {}
int operator()(int x) const { return x*m_val; }
private:
int m_val;
};
int main(int argc, char * argv[])
{
std::vector<int> data(10);
std::generate(data.begin(), data.end(), Range(0));
std::vector<int>::iterator new_end = std::remove_if(data.begin(), data.end(), compose1(std::bind2nd(std::equal_to<int>(), 0), std::bind2nd(std::modulus<int>(), 2)));
data.erase(new_end, data.end());
std::transform(data.begin(), data.end(), data.begin(), Times(2));
std::copy(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
More information about the Python-list
mailing list