perl vs c++0x Willem Hengeveld http://xs4all.nl/~itsme itsme@xs4all.nl #C++ programmers? #Boost programmers? How do I use perl I use !Gperl + __DATA__ in vim a lot Or :perldo s/re/expr/e in vim Usually prototype, experiment in perl LWP::UserAgent CGI.pm Portability is nice, but hardly ever an issue #Boost does not come close to CPAN for variety of subjects covered, but does provide a large universal set of very portable high quality libraries. #In perl i usually get something working quicker. Reasons to use c++ Out of memory with perl Speed Better compiler warnings/errors No need to install perl I needed to run 32bit code on my 64bit mac I needed to run my code on wince phones # I needed to handle +- 250M files # And i wanted to learn more about c++ # and template programming # When i send a program to a customer, they often don't have perl, and don't want to, or can't install it. How to program perl in c++ Memory management Hashes Arrays Anonymous hash vs initializer Anonymous sub vs lambda functions regexes CPAN for c++? Portability Runtime warnings # Subjects i will cover in the next slides Memory management in c++ Let the compiler create temporary objects for you Use std::string for strings Declare as: func(const std::string& str) Call as: func(“literal”) func( “literal” + str.substr(0,4)) Don't store references in structs – copy: std::map Memory management in c++ Use reference counted smart pointers With boost::shared_ptr ( or in c++0x: std::tr1::shared_ptr ) I usually declare ptrs for structs. struct { int a; } intstruct; typedef boost::shared_ptr intstruct_ptr; Then store ptr's in vectors or hashes hashes std::map Ordered map With explicit ordering function, or implicit via operator< boost::unordered_map or std::tr1::unordered_map Unordered hash Like perl hashes #An ordered map has the nice property that you can search for a value near your key, instead of only if it exists. -> The upper_bound / lower_bound methods. hashes my %x; map x; $x{a}= 1; x[“a”]= 1; Exists $x{a} x.find(“a”)!=x.end() auto-vivify auto-vifify for my $k (keys %x) ...for_each my $a={a=>1}; ...initializer_list ##### arrays my @a; std::vector a; push @a, 1; a.push_back(1); $_++ for @a; std::for_each(a.begin(), a.end(), [](int&x) { x++; }); my $a=[1,2]; Use initializer_list auto-vivify No auto vivify join < boost/algorithm/string/join.hpp > @a[2..5]; Use iterator pair ##### Array slice @a[2..5] If you want to make a copy: slice(a, 2, 5); template V slice(const V& v, size_t first, size_t last) { V w; std::copy( v.begin()+first, v.begin()+last , back_inserter(w)); return w; } Otherwise use iterator pairs ##### Anon hash vs initializer_list In perl In c++0x my $m= { Mymap m= { abc=>123, { “abc”, 123 }, klm=>456, { “klm”, 456 }, } }; my $a= [ Myvec a= { 'xyz', “xyz”, 'qwe' “qwe”, ]; }; Anon sub vs Lambda fns sub docall { template My $cb= shift; void docall(F f) { $cb->(); f(); } } my $var= “x”; std::string var= “x”; docall( sub { print $var; }); docall( [&var]() { puts(x.c_str()); }); c++0x sometimes needs some convincing to get the function type right Either use template, or boost::function<> c++0x has closures too: var ################ regex #include int main(int, char**) { boost::smatch m; if (boost::regex_match(std::string("test aaabbbqwerty"), m, boost::regex("(\\w+)\\s+(?:aaa|bbb)+(\\w+)"))) printf("1=%s, 2=%s\n", std::string(m[1]).c_str(), std::string(m[2]).c_str()); return 0; } ########### CPAN vs boost CPAN : 21000 modules http://search.cpan.org/ Boost: 105 libraries http://www.boost.org/doc/libs/ STL: part of the c++ standard http://www.sgi.com/tech/stl Cpan: wide range of topics Boost: more fundamental algorithms ########### portability In perl: don't use unpack(“Q”) Know what perl version you require to run C++: use Don't assume a ptr fits in a unsigned long Don't assume you can cast a char* to long* # Char *p; # *(long*)p may cause alignment problem Not so easy yet Difficult to understand compiler error messages when using template libraries #include #include int main(int,char**) { typedef std::map > s2v_t; s2v_t x; x["abc"].push_back(1); } Not so easy yet Difficult to understand compiler error messages when using template libraries errormsg.cpp: In function ‘int main(int, char**)’: errormsg.cpp:9: error: no match for ‘operator[]’ in ‘x["abc"]’ /usr/include/c++/4.2.1/bits/stl_map.h:342: note: candidates are: _Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string, std::allocator >, _Tp = std::vector >, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, std::vector > > >] /usr/include/c++/4.2.1/bits/stl_pair.h: At global scope: /usr/include/c++/4.2.1/bits/stl_pair.h: In instantiation of ‘std::pair, std::allocator >, std::vector > >’: /usr/include/c++/4.2.1/bits/stl_tree.h:138: instantiated from ‘std::_Rb_tree_node, std::allocator >, std::vector > > >’ /usr/include/c++/4.2.1/bits/stl_tree.h:1325: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = std::basic_string, std::allocator >, _Val = std::pair, std::allocator >, std::vector > >, _KeyOfValue = std::_Select1st, std::allocator >, std::vector > > >, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, std::vector > > >]’ /usr/include/c++/4.2.1/bits/stl_tree.h:594: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = std::basic_string, std::allocator >, _Val = std::pair, std::allocator >, std::vector > >, _KeyOfValue = std::_Select1st, std::allocator >, std::vector > > >, _Compare = std::less, std::allocator > >, _Alloc = std::allocator, std::allocator >, std::vector > > >]’ /usr/include/c++/4.2.1/bits/stl_map.h:94: instantiated from here /usr/include/c++/4.2.1/bits/stl_pair.h:73: error: ‘std::pair<_T1, _T2>::first’ has incomplete type /usr/include/c++/4.2.1/bits/stringfwd.h:56: error: declaration of ‘const struct std::basic_string, std::allocator >’ I forgot to #include here. gcc4.5 produces much better msgs ############# c++0x support Gcc since v4.4 support it with -std=c++0x Msvc 10.0 Questions?