Skip to content

Instantly share code, notes, and snippets.

@tlkahn
Created October 5, 2016 07:25
Show Gist options
  • Save tlkahn/4293bb37d3b176f692ac8a508832c84c to your computer and use it in GitHub Desktop.
Save tlkahn/4293bb37d3b176f692ac8a508832c84c to your computer and use it in GitHub Desktop.
create a closure in C++
struct db_sum: public std::unary_function<boost::any, void> {
private:
double& sum_;
public:
explicit db_sum(double& sum)
: sum_(sum)
{}
void operator()(const cell_t& value) {
const std::type_info& ti = value.type();
if (ti == typeid(int)) {
sum_ += boost::any_cast<int>(value);
} else if (ti == typeid(float)) {
sum_ += boost::any_cast<float>(value);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment