NoSenseEtAl
NoSenseEtAl

Reputation: 30078

Is it possible to write code like this in C++11 without using boost?

a while ago I stumbled upon a following line of code:

 return accumulate(s, s + size, char(), (_1 ^ _2));

It was using boost headers, but I always thought it is very very very elegant (note that lambda does not have input params named, so it is super short. :) Please note that I know C++11 has lambda functions, this is not about lambdas, it is about this nice short syntax.

Upvotes: 0

Views: 253

Answers (2)

Puppy
Puppy

Reputation: 146968

The only way to do it without Boost is to reinvent the Boost components that create it. How could you possibly have the syntax of a Boost component without an implementation of that Boost component?

The language solution is lambdas, and if you don't like them, then it's time to either use Boost or steal this specific part of it.

Upvotes: 3

David
David

Reputation: 28188

With hard coded types...

return accumulate(s, s+size, char(), [](char l, char r){ return l ^ r; });

When generic lambdas are allowed (C++14)...

return accumulate(s, s+size, char(), [](auto l, auto r){ return l ^ r; });

For now, std::bit_xor...

return accumulate(s, s+size, char(), bit_xor<char>());

Upvotes: 6

Related Questions