implement STL pretty printing using Boost HOF - c++

pretty print container of arbitrary type
The following code of few lines provides same output as prettyprint.hpp in question,
but the output stream is limited to std::cout.
How to rewrite those code using boost::hof to provide print(std::ostream&, ...) like interface?
#include <iostream>
#include <boost/hof.hpp>
BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print) = //boost::hof::proj(
boost::hof::fix(boost::hof::first_of(
[](auto, const auto& x) -> decltype(std::cout << x, void()) {
std::cout << x;
},
[](auto self, const auto& range)
-> decltype(self(*std::begin(range)), void()) {
bool sep = false;
std::cout << '{';
for (const auto& x : range) {
if (sep)
std::cout << ',';
else
sep = true;
self(x);
}
std::cout << '}';
},
[](auto self, const auto& tuple) {
using namespace boost::hof;
std::cout << '(';
bool sep = false;
unpack(proj([&](const auto& i) {
if (sep)
std::cout << ',';
else
sep = true;
self(i);
}))(tuple);
std::cout << ')';
}));//})));
template <typename... Args>
void print(Args&&... args) {
simple_print(std::make_tuple(std::forward<Args>(args)...));
}
//---- user code ---
struct XX {
int n = 0;
friend std::ostream& operator<<(std::ostream& os, const XX& o) {
return os << o.n << "XX";
}
};
int main() {
std::vector v = {1, 2, 3, 4};
std::map<std::string, int> m = {{"a", 30}, {"bb", 31}, {"ccc", 32}};
auto t = std::make_tuple(6, 7, 8, 9);
auto t2 = std::make_tuple(11, std::ref(v), t);
auto t3 = std::make_tuple(t2, std::vector{1234, 23, 2, 3, 3}, "abc",
std::vector{
std::vector{11, 12, 13}, std::vector{15, 16, 17}, std::vector{19}});
print(t3, "xxxx", 55, m, std::vector<std::string>{"x"}, XX{66});
// (((11, [1, 2, 3, 4], (6, 7, 8, 9)), [1234, 23, 2, 3, 3], abc, [[11, 12,
// 13], [15, 16, 17], [19]]), xxxx, 55, [(a, 30), (bb, 31), (ccc, 32)], [x],
// 66XX)
}

Just adding a parameter to pass the ostream would suffice:
BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print_ex) = boost::hof::fix(
boost::hof::first_of(
[](auto, auto& os, const auto &x) -> decltype(os << x, void()) { os << x; },
[](auto self, auto& os, const auto &range) -> decltype(self(os, *std::begin(range)), void()) {
bool sep = false;
os << '{';
for (const auto &x : range) {
sep = !sep || os << ',';
self(os, x);
}
os << '}';
},
[](auto self, auto& os, const auto &tuple) {
using namespace boost::hof;
os << '(';
bool sep = false;
unpack(proj([&](const auto &i) {
sep = !sep || os << ',';
self(os, i);
}))(tuple);
os << ')';
}));
template <typename Ostream, typename... Args> void print_ex(Ostream& os, Args &&... args) { simple_print_ex(os, std::make_tuple(std::forward<Args>(args)...)); }
Now you can use it like so:
Live On Wandbox
std::ofstream ofs("test.txt");
print_ex(ofs, t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
ofs << "\n";
Of course, the old print can be a trivial forwarding wrapper now:
template <typename... Args> void print(Args &&... args) {
print_ex(std::cout, std::forward<Args>(args)...);
}
Listing
Live On Wandbox
#include <boost/hof.hpp>
#include <iostream>
BOOST_HOF_STATIC_LAMBDA_FUNCTION(simple_print_ex) = boost::hof::fix(
boost::hof::first_of(
[](auto, auto& os, const auto &x) -> decltype(os << x, void()) { os << x; },
[](auto self, auto& os, const auto &range) -> decltype(self(os, *std::begin(range)), void()) {
bool sep = false;
os << '{';
for (const auto &x : range) {
sep = !sep || os << ',';
self(os, x);
}
os << '}';
},
[](auto self, auto& os, const auto &tuple) {
using namespace boost::hof;
os << '(';
bool sep = false;
unpack(proj([&](const auto &i) {
sep = !sep || os << ',';
self(os, i);
}))(tuple);
os << ')';
}));
template <typename Ostream, typename... Args> void print_ex(Ostream& os, Args &&... args) { simple_print_ex(os, std::make_tuple(std::forward<Args>(args)...)); }
template <typename... Args> void print(Args &&... args) { print_ex(std::cout, std::forward<Args>(args)...); }
//---- user code ---
struct XX {
int n = 0;
friend std::ostream &operator<<(std::ostream &os, const XX &o) { return os << o.n << "XX"; }
};
#include <map>
#include <vector>
#include <fstream>
int main() {
using namespace std::string_literals;
std::vector v = { 1, 2, 3, 4 };
std::map m { std::pair { "a"s, 30 }, { "bb", 31 }, { "ccc", 32 } };
auto t = std::make_tuple(6, 7, 8, 9);
auto t2 = std::make_tuple(11, std::ref(v), t);
auto t3 = std::make_tuple(t2, std::vector{ 1234, 23, 2, 3, 3 }, "abc",
std::vector{ std::vector{ 11, 12, 13 }, std::vector{ 15, 16, 17 }, std::vector{ 19 } });
std::ofstream ofs("test.txt");
print_ex(ofs, t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
ofs << "\n";
print(t3, "xxxx", 55, m, std::vector<std::string>{ "x" }, XX{ 66 });
}

Related

Print function in C++ similar to Python

I am trying to make a header file that makes it easy to learn C++.
I was wondering if there was any way to make a print function in C++ that behaved similar to the print function in Python which prints the data irrespective of the datatype or dimensions of the data passed to it.
I could make a function that could print any numeric types and strings using templates and function overloading. But I am struggling with vectors and arrays as they may be multi dimensional.
I could get it to print upto 3D vectors using this:
#include <iostream>
#include <vector>
template<typename T>
void printVector(const std::vector<T>& vec)
{
for (const auto& element : vec)
std::cout << element << ' ';
std::cout << '\n';
}
template<typename T>
void printVector(const std::vector<std::vector<T>>& vec)
{
for (const auto& innerVector : vec)
printVector(innerVector);
}
int main()
{
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<std::vector<double>> vec2{ { 1.1, 2.2 }, { 3.3, 4.4 } };
std::vector<std::vector<std::vector<char>>> vec3{ { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } };
printVector(vec1);
printVector(vec2);
printVector(vec3);
return 0;
}
You can do something like this:
#include <iostream>
#include <vector>
#include <map>
namespace detail {
template<typename T>
void printImpl(const T& o)
{
std::cout << o;
}
template<typename T>
void printImpl(const std::vector<T>& vec)
{
char sep = '{';
for (const auto& innerVector : vec) {
std::cout << sep;
printImpl(innerVector);
sep = ',';
}
std::cout << "}";
}
template<typename K, typename V>
void printImpl(const std::map<K, V>& cont)
{
char sep = '{';
for (auto&& [k, v] : cont) {
std::cout << sep;
printImpl(k);
std::cout << ":";
printImpl(v);
sep = ',';
}
std::cout << "}";
}
}
template<typename... Args>
void print(Args&&... args)
{
(detail::printImpl(std::forward<Args>(args)), ...);
}
int main()
{
std::vector<int> vec1{ 1, 2, 3, 4, 5 };
std::vector<std::vector<double>> vec2{ { 1.1, 2.2 }, { 3.3, 4.4 } };
std::vector<std::vector<std::vector<char>>> vec3{ { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } };
std::map<int, std::string> m{{0, "a"},{1, "b"}};
print(vec1,"\n");
print(vec2,"\n");
print(vec3,"\n");
print(m,"\n");
return 0;
}
And specialize detail::printImpl() for any type for which you don't like default operator <<() behavior.

Is there any shortcut method to print 2D-array c++

Is there any shortcut method in c++ to output 2d array(i.e. apart from for loop)?
Is there a special function in STL to output it.
Well, since you mentioned STL functions, you could use the std::for_each function with lambda functions to print the 2D array:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int matrix[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
auto elem_printer = [](int num) { std::cout << num << " "; };
auto row_printer = [&elem_printer](int (&row)[3]) {
std::for_each(std::begin(row),std::end(row),elem_printer);
std::cout << std::endl;
};
std::for_each(std::begin(matrix),std::end(matrix),row_printer);
}
However, this is exactly the same as two for loops, but uglier.
I have this template for streams, which hides away some of the ugliness, and you benefit from it being reusable and it handles multiple dimensions. There is no way to get away without doing the loops somewhere, of course:
template <class Stream, size_t depth>
class Pretty
{
Stream& s;
public:
Pretty(Stream& s): s(s) {}
template <size_t d1, typename T>
Stream& operator <<( T const (&v)[d1])const
{
const char* sep = "{";
for (auto& m : v)
{
s << sep << m;
sep = ", ";
}
s << "}";
return s;
}
template <size_t d1, typename T, size_t d2>
std::ostream& operator <<(T const (&v)[d1][d2])const
{
enum {DENT = 4};
std::string dent (DENT,' ');
std::string indent(depth*DENT,' ');
std::string sep = "{\n" + indent + dent;
for (auto& m : v)
{
s << sep; Pretty<Stream,depth+1>(s) << m;
sep = ",\n" + indent + dent;
}
s << "\n" << indent << "}";
return s;
}
};
class PrettyNext
{};
Pretty<std::ostream,0> operator << (std::ostream& s, const PrettyNext&)
{
return Pretty<std::ostream,0>(s);
}
And usage:
int i [][3][2] = { { {1,2}, {3,4}, {5,6} },{{0}}};
std::cout << "This is a test:\n" << PrettyNext() << i << std::endl;
Output is:
This is a test:
{
{
{1, 2},
{3, 4},
{5, 6}
},
{
{0, 0},
{0, 0},
{0, 0}
}
}
I have been fighting to get this to work directly on std::ostream, but there is a collision with the standard char* handling I can't quite resolve.

Print comma separated list from std::vector [duplicate]

This question already has answers here:
How can I print a list of elements separated by commas?
(34 answers)
Closed 2 years ago.
I'm trying to print a comma separated list of a single detail from a std::vector<MyClass>. So far the simplest and cleverest way I have seen to do this is to use
std::ostringstream ss;
std::copy(vec.begin(), vec.end() - 1, std::ostream_iterator<std::string>(ss, ", "))
ss << vec.back();
That worked fine when I was printing a vector of strings. However, now I am trying to print a single detail about MyClass. I know in Python I could do something like
(x.specific_detail for x in vec)
to get a generator expression for the thing that I am interested in. I'm wondering if I can do something similar here or if I am stuck doing
for (auto it = vec.begin(); it != vec.end(); ++it) {
// Do stuff here
}
One way of solving this I have seen is:
std::string separator;
for (auto x : vec) {
ss << separator << x.specific_detail;
separator = ",";
}
A fairly easy and reusable way:
#include <vector>
#include <iostream>
template<class Stream, class T, class A>
Stream& printem(Stream&os, std::vector<T, A> const& v)
{
auto emit = [&os, need_comma = false](T const& x) mutable
{
if (need_comma) os << ", ";
os << x;
need_comma = true;
};
for(T const& x : v) emit(x);
return os;
}
int main()
{
auto v = std::vector<int> { 1, 2, 3, 4 , 5 };
printem(std::cout, v) << std::endl;
}
And another way which defines an extendable protocol for printing containers:
#include <vector>
#include <iostream>
template<class Container>
struct container_printer;
// specialise for a class of container
template<class T, class A>
struct container_printer<std::vector<T, A>>
{
using container_type = std::vector<T, A>;
container_printer(container_type const& c) : c(c) {}
std::ostream& operator()(std::ostream& os) const
{
const char* sep = "";
for (const T& x : c) {
os << sep << x;
sep = ", ";
}
return os;
}
friend std::ostream& operator<<(std::ostream& os, container_printer const& cp)
{
return cp(os);
}
container_type c;
};
template<class Container>
auto print_container(Container&& c)
{
using container_type = typename std::decay<Container>::type;
return container_printer<container_type>(c);
}
int main()
{
auto v = std::vector<int> { 1, 2, 3, 4 , 5 };
std::cout << print_container(v) << std::endl;
}
...of course we can go further...
#include <vector>
#include <iostream>
template<class...Stuff>
struct container_printer;
// specialise for a class of container
template<class T, class A, class Separator, class Gap, class Prefix, class Postfix>
struct container_printer<std::vector<T, A>, Separator, Gap, Prefix, Postfix>
{
using container_type = std::vector<T, A>;
container_printer(container_type const& c, Separator sep, Gap gap, Prefix prefix, Postfix postfix)
: c(c)
, separator(sep)
, gap(gap)
, prefix(prefix)
, postfix(postfix) {}
std::ostream& operator()(std::ostream& os) const
{
Separator sep = gap;
os << prefix;
for (const T& x : c) {
os << sep << x;
sep = separator;
}
return os << gap << postfix;
}
friend std::ostream& operator<<(std::ostream& os, container_printer const& cp)
{
return cp(os);
}
container_type c;
Separator separator;
Gap gap;
Prefix prefix;
Postfix postfix;
};
template<class Container, class Sep = char, class Gap = Sep, class Prefix = char, class Postfix = char>
auto print_container(Container&& c, Sep sep = ',', Gap gap = ' ', Prefix prefix = '[', Postfix postfix = ']')
{
using container_type = typename std::decay<Container>::type;
return container_printer<container_type, Sep, Gap, Prefix, Postfix>(c, sep, gap, prefix, postfix);
}
int main()
{
auto v = std::vector<int> { 1, 2, 3, 4 , 5 };
// json-style
std::cout << print_container(v) << std::endl;
// custom
std::cout << print_container(v, " : ", " ", "(", ")") << std::endl;
// custom
std::cout << print_container(v, "-", "", ">>>", "<<<") << std::endl;
}
expected output:
[ 1,2,3,4,5 ]
( 1 : 2 : 3 : 4 : 5 )
>>>1-2-3-4-5<<<
Here's an example using std::transform:
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
int main()
{
std::vector<std::string> strs = {"Testing", "One", "Two", "Three"};
if (!strs.empty())
{
std::copy(std::begin(strs), std::prev(std::end(strs)), std::ostream_iterator<std::string>(std::cout, ", "));
std::cout << strs.back();
}
std::cout << '\n';
if (!strs.empty())
{
std::transform(std::begin(strs), std::prev(std::end(strs)), std::ostream_iterator<size_t>(std::cout, ", "),
[](const std::string& str) { return str.size(); });
std::cout << strs.back().size();
}
std::cout << '\n';
}
Output:
Testing, One, Two, Three
7, 3, 3, 5
Here is a tiny simple range library:
template<class It>
struct range_t {
It b, e;
It begin() const { return b; }
It end() const { return e; }
bool empty() const { return begin()==end(); }
std::size_t size() const { return std::distance( begin(), end() ); }
range_t without_front( std::size_t n = 1 ) const {
n = (std::min)(size(), n);
return {std::next(b, n), e};
}
range_t without_back( std::size_t n = 1 ) const {
n = (std::min)(size(), n);
return {b, std::prev(e, n)};
}
range_t only_front( std::size_t n = 1 ) const {
n = (std::min)(size(), n);
return {b, std::next(b, n)};
}
range_t only_back( std::size_t n = 1 ) const {
n = (std::min)(size(), n);
return {std::prev(end(), n), end()};
}
};
template<class It>
range_t<It> range(It s, It f) { return {s,f}; }
template<class C>
auto range(C&& c) {
using std::begin; using std::end;
return range( begin(c), end(c) );
}
now we are ready.
auto r = range(vec);
for (auto& front: r.only_front()) {
std::cout << front.x;
}
for (auto& rest: r.without_front()) {
std::cout << "," << rest.x;
}
Live example.
Now you can get fancier. boost transform iterators, together with boost range, let you do something similar to a list comprehension in python. Or Rangesv3 library for C++2a.
Writing a transform input iterator isn't amazingly hard, it is just a bunch of boilerplate. Simply look at the axioms of input iterator, write a type that stores an arbitrary iterator and forwards most methods to it.
It also stores some function. On * and ->, call the function on the dereferenced iterator.
template<class It, class F>
struct transform_iterator_t {
using reference=std::result_of_t<F const&(typename std::iterator_traits<It>::reference)>;
using value_type=reference;
using difference_type=std::ptrdiff_t;
using pointer=value_type*;
using iterator_category=std::input_iterator_tag;
using self=transform_iterator_t;
It it;
F f;
friend bool operator!=( self const& lhs, self const& rhs ) {
return lhs.it != rhs.it;
}
friend bool operator==( self const& lhs, self const& rhs ) {
return !(lhs!=rhs);
}
self& operator++() {
++it;
return *this;
}
self operator++(int) {
auto r = *this;
++*this;
return r;
}
reference operator*() const {
return f(*it);
}
pointer operator->() const {
// dangerous
return std::addressof( **this );
}
};
template<class F>
auto iterator_transformer( F&& f ) {
return [f=std::forward<F>(f)](auto it){
return transform_iterator_t<decltype(it), std::decay_t<decltype(f)>>{
std::move(it), f
};
};
}
template<class F>
auto range_transfromer( F&& f ) {
auto t = iterator_transformer(std::forward<F>(f));
return [t=std::move(t)](auto&&...args){
auto tmp = range( decltype(args)(args)... );
return range( t(tmp.begin()), t(tmp.end()) );
};
}
Live example of transformer.
And if we add -- we can even use ostream iterator.
Note that std::prev requires a bidirectional iterator, which requires forward iterator concept, which requires that the transform iterator return an actual reference, which is a pain.
You can use the exact code you already have, just change the type you pass to std::ostream_iterator to restrict its output:
class MyClassDetail {
const MyClass &m_cls;
public:
MyClassDetail(const MyClass &src) : m_cls(src) {}
friend std::ostream& operator<<(std::ostream &out, const MyClassDetail &in) {
return out << in.m_cls.specific_detail;
}
};
std::copy(vec.begin(), vec.end()-1, std::ostream_iterator<MyClassDetail>(ss, ", "));
ss << MyClassDetail(vec.back());
Live demo
Here's what was ultimately used
// assume std::vector<MyClass> vec
std::ostringstream ss;
std::for_each(vec.begin(), vec.end() - 1,
[&ss] (MyClass &item) {
ss << item.specific_detail << ", ";
}
);
ss << vec.back().specific_detail;
You can simply the exact same code, but define a operator<< overload:
ostream &operator<<(ostream& out)
{
out << m_detail;
}

Square and round brackets operator, how to choose overloads?

I want to access to some class data using operator[] but depending on the index type into the square brackets return one kind of data or other. As a simplified example:
struct S
{
int &operator []( int index ) { std::cout << "[i]"; return i_buffer[index]; }
short &operator [](short index) { std::cout << "[s]"; return s_buffer[index]; }
private:
int i_buffer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_buffer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
There's no way to write a short literal, so the only way to choose the short overload is by casting:
S s;
std::cout << s[9] << '\n'; // prints [i]9
std::cout << s[(short)9] << '\n'; // prints [s]999
But I don't like it and I was wondering if there's different options.
What I've tried?
Tagged parameter.
First I've tried to use "tags":
struct S
{
enum class i_type : std::int32_t {};
enum class s_type : std::int32_t {};
int &operator [](i_type index)
{ std::cout << "[i]"; return i_buffer[static_cast<int>(index)]; }
short &operator [](s_type index)
{ std::cout << "[s]"; return s_buffer[static_cast<int>(index)]; }
private:
int i_buffer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_buffer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
That works but is still a little verbose:
S s;
std::cout << s[9] << '\n'; // error, no possible overload to be taken
std::cout << s[S::i_type{9}] << '\n'; // prints [i]9
std::cout << s[S::s_type{9}] << '\n'; // prints [s]999
Template.
As a crazy workaround I wanted to try to template the operator:
struct S
{
template <typename T>
T &operator [](T) { std::cout << "???"; return 0; }
private:
int i_buffer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_buffer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
template <>
int &S::operator [](int index) { std::cout << "[i]"; return i_buffer[index]; }
template <>
short &S::operator [](short index) { std::cout << "[s]"; return s_buffer[index]; }
The template version behaves as the original code, but there's no easy way to specify a type parameter along with the operator[]:
S s;
std::cout << s[9] << '\n'; // prints [i]9 like before
std::cout << s[(short)9] << '\n'; // prints [s]999 like before
std::cout << s<short>[9] << '\n'; // s is not template
std::cout << s[9]<short> << '\n'; // nonsense
// Correct but utterly verbose and hard to write and read
std::cout << s.operator[]<short>(9) << '\n';
Question.
All the issues described also happens with operator(), I want to know if there's more alternatives that I'm not aware of?
I think that using a named method is a much better idea than using operator[] in your situation, as it would be easier to understand that two separate buffers are being accessed by reading the source code.
Regardless, if you want to use your operator[] approach, you could use strong typedefs and user defined literals to have type-safety with minimal syntactic overhead:
BOOST_STRONG_TYPEDEF(std::size_t, int_index)
BOOST_STRONG_TYPEDEF(std::size_t, short_index)
struct S
{
auto& operator[](int_index i) { /* ... */ }
auto& operator[](short_index i) { /* ... */ }
};
auto operator "" _ii(unsigned long long int x) { return int_index{x}; }
auto operator "" _si(unsigned long long int x) { return short_index{x}; }
You can then call your methods as follows:
S s;
auto& some_int = s[15_ii];
auto& some_short = s[4_si];
wandbox example
I think I'd use std::tie from the <tuple> library and then write a little helper to find the correct reference type:
#include <tuple>
#include <iostream>
template<class As, class...Ts>
auto& as(std::tuple<const Ts&...>ts)
{
return std::get<As const&>(ts);
};
template<class As, class...Ts>
auto& as(std::tuple<Ts&...>ts)
{
return std::get<As &>(ts);
};
struct S
{
// both cost and mutable version provided for completeness.
auto operator[](std::size_t i) const {
return std::tie(i_buffer[i], s_buffer[i]);
}
auto operator[](std::size_t i) {
return std::tie(i_buffer[i], s_buffer[i]);
}
private:
int i_buffer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_buffer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
int main()
{
auto s = S();
const auto x = S();
std::cout << "short is : " << as<short>(s[5])<< '\n';
std::cout << "int is : " << as<int>(s[5])<< '\n';
std::cout << "short is : " << as<short>(x[6])<< '\n';
std::cout << "int is : " << as<int>(x[6])<< '\n';
}
This way, the code is explicit but still succinct.
expected output:
short is : 555
int is : 5
short is : 666
int is : 6
Having read the further comments, I might choose to store the matrix in (say) row-wise form and then provide a col-wise wrapper.
A barely functional example:
#include <tuple>
#include <iostream>
#include <array>
template<std::size_t Rows, std::size_t Cols>
struct RowWiseMatrix
{
auto& operator[](std::size_t i) { return data_[i]; }
std::array<std::array<double, Cols>, Rows> data_;
};
template<std::size_t Rows, std::size_t Cols>
struct ColumnProxy
{
ColumnProxy(std::array<std::array<double, Cols>, Rows>& data, std::size_t col)
: data_(data), col_(col)
{
}
auto& operator[](std::size_t i) { return data_[i][col_]; }
std::array<std::array<double, Cols>, Rows>& data_;
std::size_t col_;
};
template<std::size_t Rows, std::size_t Cols>
struct ColWiseProxy
{
ColWiseProxy(RowWiseMatrix<Rows, Cols>& mat) : underlying_(mat) {}
auto operator[](std::size_t i) { return ColumnProxy<Rows, Cols> { underlying_.data_, i }; }
RowWiseMatrix<Rows, Cols>& underlying_;
};
template<std::size_t Rows, std::size_t Cols>
auto& rowWise(RowWiseMatrix<Rows, Cols>& mat)
{
return mat;
};
template<std::size_t Rows, std::size_t Cols>
auto colWise(RowWiseMatrix<Rows, Cols>& mat)
{
return ColWiseProxy<Rows, Cols>(mat);
};
int main()
{
auto m = RowWiseMatrix<3, 3> {
std::array<double, 3>{ 1, 2, 3 },
std::array<double, 3>{ 4, 5, 6},
std::array<double, 3>{ 7, 8, 9}
};
std::cout << rowWise(m)[0][2] << '\n';
std::cout << colWise(m)[0][2] << '\n';
}
Expected output:
3
7
I agree with Vittorio Romeo that the best solution is a named method.
However here is a solution:
template <class T> struct S_proxy {
T* data;
T& operator[](std::size_t i) { return data[i]; }
};
struct S
{
auto i_type() { return S_proxy<int>{i_buffer}; };
auto s_type() { return S_proxy<short>{s_buffer}; };
private:
int i_buffer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_buffer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
and use:
S s;
return s.s_type()[2];
If i_type and s_type are supposed to have a meaning by themselves, it is possible to add semantics to operators []. Something like
#include <iostream>
struct Month {
explicit Month(int m)
: m(m)
{
}
int m;
};
struct Day {
explicit Day(short d)
: d(d)
{
}
short d;
};
struct S {
int& operator[](const Month& mes)
{
std::cout << "[i]";
return i_bufer[mes.m];
}
short& operator[](const Day& dis)
{
std::cout << "[s]";
return s_bufer[dis.d];
}
private:
int i_bufer[10]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
short s_bufer[10]{ 0, 111, 222, 333, 444, 555, 666, 777, 888, 999 };
};
int main()
{
S s;
std::cout << s[Month(9)] << '\n'; // muestra [i]9
std::cout << s[Day(9)] << '\n'; // muestra [s]999
}

Boost-range not working with C++1y init-capture mutable lambda

I want to compute the element-wise difference of two vectors using Boost.Range and C++1y lambdas with init-capture. The simpler case of subtracting a fixed (i.e. the first) element of one vector works. However, when I try to compute the "vectorized difference" by increasing the iterator over the second range (and making the lambda mutable), I get a compiler error. Sample code (note that I didn't use generalized lambdas so that both g++ 4.8 and Clang SVN can parse this code):
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
template<class R>
auto delta_beg(R const& rng1, R const& rng2)
{
using Elem = typename R::value_type;
return rng1 | boost::adaptors::transformed(
[first2 = begin(rng2)](Elem const& e) {
return e - *first2;
});
}
template<class R>
auto delta_rng(R const& rng1, R const& rng2)
{
using Elem = typename R::value_type;
return rng1 | boost::adaptors::transformed(
[first2 = begin(rng2)](Elem const& e) mutable {
return e - *first2++;
});
}
int main()
{
auto r1 = std::vector<int>{ 8, 10, 12, 15 };
auto r2 = std::vector<int>{ 1, 2, 9, 13 };
// prints 7, 9, 11, 14
boost::copy(delta_beg(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
// ERROR, should print 7, 8, 3, 2
boost::copy(delta_rng(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
}
Live Example. Here both g++ and Clang complain about
no type named 'type' in
'boost::mpl::eval_if, boost::result_of]::__lambda1(const int&)>,
boost::mpl::identity >::f_ {aka struct
boost::result_of]::__lambda1(const int&)>}'
typedef typename f_::type type;
Question: what is going on?
It's just that closure-types don't have nested typedefs that boost::mpl apparently requires. It works if you convert the lambda expression to std::function:
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
template<class R>
auto delta_beg(R const& rng1, R const& rng2)
{
using Elem = typename R::value_type;
std::function<Elem(Elem const&)> f =
[first2 = begin(rng2)](Elem const& e) { return e - *first2; };
return rng1 | boost::adaptors::transformed(f);
}
template<class R>
auto delta_rng(R const& rng1, R const& rng2)
{
using Elem = typename R::value_type;
std::function<Elem(Elem const&)> f =
[first2 = begin(rng2)](Elem const& e) mutable { return e - *first2++; };
return rng1 | boost::adaptors::transformed(f);
}
int main()
{
auto r1 = std::vector<int>{ 8, 10, 12, 15 };
auto r2 = std::vector<int>{ 1, 2, 9, 13 };
// prints 7, 9, 11, 14
boost::copy(delta_beg(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
// ERROR, should print 7, 8, 3, 2
boost::copy(delta_rng(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
}
Live demo.