?? karlsson.txt
字號:
Lambda Expressions & C++
by Bjorn Karlsson
Listing 1:
template <typename T, typename Stream> class print_to_stream_t {
Stream& stream_;
public:
print_to_stream_t(Stream& s):stream_(s) {}
void operator()(const T& t) const {
stream_ << t;
}
};
template <typename T,typename Stream>
print_to_stream_t<T,Stream> print_to_stream(Stream& s) {
return print_to_stream_t<T,Stream>(s);
}
Listing 2:
#include <boost/lambda/lambda.hpp>
int main() {
using namespace boost::lambda;
std::vector<std::string> vec;
vec.push_back("This");
vec.push_back(" is");
vec.push_back(" extremely");
vec.push_back(" cool.");
std::for_each(vec.begin(),vec.end(),std::cout << _1);
}
Listing 3:
namespace sample {
struct placeholder_1 {};
placeholder_1 _1;
template <typename T> struct simple_binder_lshift {
T& t_;
public:
simple_binder_lshift(T& t):t_(t) {}
template <typename U> T& operator()(U u) {
t_ << u;
return t_;
}
};
}
template <typename T> sample::simple_binder_lshift<T>
operator<<(T& t,sample::placeholder_1 ignore) {
return sample::simple_binder_lshift<T>(t);
}
int main() {
using sample::_1;
std::vector<std::string> vec;
vec.push_back("Simple");
vec.push_back(" example");
std::for_each(vec.begin(),vec.end(),std::cout << _1);
}
Listing 4:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
int main() {
using namespace boost::lambda;
std::vector<int> vec;
for(int i=1;i<50;++i) {
vec.push_back(i);
}
std::cout << "Print the odd numbers\n";
std::for_each(
vec.begin(),
vec.end(),
if_then(_1%2,std::cout << constant(" ") << _1));
std::cout <<
"\n\nIf the number is even, double it, otherwise print it\n";
std::for_each(
vec.begin(),
vec.end(),
if_(_1%2==0)[_1*=2].else_[std::cout << constant(" ") << _1]);
std::cout << "\nVerify the above\n";
constant_type<char>::type space(constant(' '));
std::for_each(
vec.begin(),
vec.end(),
if_(_1%2==0)[std::cout << space << _1]);
}
Listing 5:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/exceptions.hpp>
#include <boost/lambda/bind.hpp>
class missile_error : public std::exception {
public:
virtual const char* what() {
return "This missile has gone bad.";
}
};
class missile {
bool broken_;
public:
void fire() {
if (broken_)
throw missile_error();
std::cout << "Gone like the wind\n";
}
void destroy() {
broken_=true;
}
};
int main() {
using namespace boost::lambda;
std::vector<missile> vec;
for (int i=0;i<10;++i)
vec.push_back(missile());
vec[4].destroy();
// Here we go
for_each(
vec.begin(),
vec.end(),
try_catch(
bind(&missile::fire,_1),catch_exception<missile_error>(
std::cout << constant("Caught missile_error: ")
<< bind(&std::exception::what, _e))));
}
Listing 6:
#include <boost/any.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/exceptions.hpp>
#include <boost/lambda/bind.hpp>
int main() {
using namespace boost::lambda;
std::vector<boost::any> stuff;
stuff.push_back(std::string("This"));
stuff.push_back(42);
stuff.push_back(std::string("is"));
stuff.push_back(3.14159265);
stuff.push_back(std::string("neat"));
constant_type<char>::type noop(constant(' '));
std::for_each(
stuff.begin(),
stuff.end(),
try_catch(
(
std::cout <<
bind<std::string>(
&boost::any_cast<std::string>,_1) << ' ', noop),
catch_exception<boost::bad_any_cast>(
// Eat the exception
noop
)
));
}
3
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -