How to use boost::assign with custom containers that extend STL containers? - c++

What do I have to do to make custom classes (that inherit from STL containers like std::vector or std::map) work with the boost::assign list_of() or map_list_of() initializer functions?
Background
I want to easily initialize a containers with a list of values. C++11 introduced initializer lists however I am stuck with C++03 so I cannot use C++11 initializer lists.
As a workaround I found the boost:assign library that provides functions like list_of() and map_list_of(). This works quite well for the STL containers like std::vector and std::map. However if I create my own containers by e.g. by extending std::vector I get compilation errors.
Example
Here is a small example
#include "boost/assign/list_of.hpp"
using namespace boost::assign;
#include <vector>
struct SpecialVector : public std::vector<int>{
foo(){/* adds functionality */}
};
int main(){
std::vector<int> v = list_of(1)(2)(3); // list_of() works well for STL containers
// The following works but requires adding items one-by-one with push_back
SpecialVector u;
u.push_back(1);
u.push_back(2);
u.push_back(3);
// The following fails when attempting to compile
SpecialVector u2 = list_of(1)(2)(3);
}
Attempting to compile the example gives me the following error:
In file included from assign_inherited.cpp:1:0:
../../../lib/boost/assign/list_of.hpp: In instantiation of 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert(const Container*, boost::assign_detail::default_type_tag) const [with Container = SpecialVector; DerivedTAssign = boost::assign_detail::generic_list<int>; Iterator = std::_Deque_iterator<int, int&, int*>]':
../../../lib/boost/assign/list_of.hpp:142:38: required from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert_to_container() const [with Container = SpecialVector; DerivedTAssign = boost::assign_detail::generic_list<int>; Iterator = std::_Deque_iterator<int, int&, int*>]'
../../../lib/boost/assign/list_of.hpp:436:81: required from 'boost::assign_detail::generic_list<T>::operator Container() const [with Container = SpecialVector; T = int]'
assign_inherited.cpp:19:39: required from here
../../../lib/boost/assign/list_of.hpp:163:20: error: no matching function for call to 'SpecialVector::SpecialVector(boost::assign_detail::converter<boost::assign_detail::generic_list<int>, std::_Deque_iterator<int, int&, int*> >::iterator, boost::assign_detail::converter<boost::assign_detail::generic_list<int>, std::_Deque_iterator<int, int&, int*> >::iterator)'
return Container( begin(), end() );
^~~~~~~~~~~~~~~~~~~~~~~~~~~
assign_inherited.cpp:5:8: note: candidate: SpecialVector::SpecialVector()
struct SpecialVector : public std::vector<int>{
^~~~~~~~~~~~~
assign_inherited.cpp:5:8: note: candidate expects 0 arguments, 2 provided
assign_inherited.cpp:5:8: note: candidate: SpecialVector::SpecialVector(const SpecialVector&)
assign_inherited.cpp:5:8: note: candidate expects 1 argument, 2 provided
I already checked the documentation of the boost::assign library. I found the section Extending the library, however if I understand it correctly, this section deals with adding custom classes as items in the list, not with generating an initializer for a custom class. Or did I understand this wrong?

Like you said, you need to allow the construction from base types:
Live On Coliru
#include "boost/assign/list_of.hpp"
using namespace boost::assign;
#include <vector>
struct SpecialVector : std::vector<int>{
typedef std::vector<int> base;
void foo(){/* adds functionality */}
SpecialVector() : base() {}
template <typename T> explicit SpecialVector(T const& t) : base(t) {}
template <typename T, typename U> SpecialVector(T const& t, U const& u) : base(t, u) {}
template <typename T, typename U, typename V> SpecialVector(T const& t, U const& u, V const& v) : base(t, u, v) {}
};
int main(){
std::vector<int> v = list_of(1)(2)(3); // list_of() works well for STL containers
// The following works but requires adding items one-by-one with push_back
SpecialVector u;
u.push_back(1);
u.push_back(2);
u.push_back(3);
// The following fails when attempting to compile
SpecialVector u2 = list_of(1)(2)(3);
}

Related

Creating a vector of the type of std::any

Consider the following example
#include <iostream>
#include <any>
#include <vector>
#include <map>
#include <typeinfo>
typedef enum TYPE{
INT8=0,
INT16=1,
INT32=2
} TYPE;
int main()
{
std::map<TYPE, std::any> myMap;
myMap[TYPE::INT8] = (int8_t)0;
myMap[TYPE::INT16] = (int16_t)0;
myMap[TYPE::INT32] = (int32_t)0;
std::vector<decltype(myMap[TYPE::INT8])> vec;
}
I have a map in this example, going from some enum to std::any. I actually need a flexible data structure that can map from a specific type (enum TYPE in this case), to multiple data types (different types of int), hence the use of std::any.
Going ahead, I would like to ascertain the type of value given for the key and construct a vector with it. I tried the above code, and it runs into a compilation error because decltype will return std::any(correctly so).
I would want to extract the "true type" from the std::any and create that type of vector. How would I achieve that.
A small snippet of the compilation error is as follows -
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/new_allocator.h:63:26: error: forming pointer to reference type 'std::any&'
63 | typedef _Tp* pointer;
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/new_allocator.h:112:7: error: forming pointer to reference type 'std::any&'
112 | allocate(size_type __n, const void* = static_cast<const void*>(0))
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_vector.h:1293:7: error: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::any&; _Alloc = std::allocator<std::any&>; value_type = std::any&]' cannot be overloaded with 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::any&; _Alloc = std::allocator<std::any&>; value_type = std::any&]'
1293 | push_back(value_type&& __x)
TIA
As suggested in the comments by #Ted Lyngmo, I think std::variant serves you better. Especially with C++-20's templated lambdas, the std::visit function can work wonders with these to get around the awkwardness of dealing with type enums and the like.
Note that you can not get around the runtime type detection. In any case, here is an example of how it can work.
#include <cstdint>
#include <iostream>
#include <variant>
#include <vector>
using VariantScalar = std::variant<
std::int8_t, std::int16_t, std::int32_t>;
using VariantVector = std::variant<
std::vector<std::int8_t>,
std::vector<std::int16_t>,
std::vector<std::int32_t>>;
VariantVector fill_vector(VariantScalar scalar, std::size_t n)
{
auto make_vector = [n]<class IntType>(IntType v) -> VariantVector {
return std::vector<IntType>(n, v);
};
return std::visit(make_vector, scalar);
}
void print_vector(const VariantVector& vec)
{
std::visit([]<class T>(const std::vector<T>& vec) {
for(const T& s: vec)
std::cout << s << ' ';
std::cout << '\n';
}, vec);
}
int main()
{
VariantScalar s(std::int8_t(1));
VariantVector vec = fill_vector(s, 5);
print_vector(vec);
}
Assuming you have the following enum definition:
enum class TYPE{
INT8=0,
INT16=1,
INT32=2
};
Then you can define a helper:
template <TYPE>
struct my_type {}; // Base case
template <>
struct my_type<TYPE::INT8> {
using type = int8_t;
};
template <>
struct my_type<TYPE::INT16> {
using type = int16_t;
};
template <>
struct my_type<TYPE::INT32> {
using type = int32_t;
};
template <TYPE t>
using my_type = typename my_type<t>::type;
That you can use for your vector
std::vector<my_type<TYPE::INT8>> vec;

Boost mp11 mp_for_each with additional function argument

I'm trying to pass a list of pairs of types constructed from boost::mp11::mp_product to a function that takes the pairs along with an additional function argument via boost::mp11::mp_for_each.
The docs I've found for mp_for_each are limited to use with generic lambdas or pure functions, so I can't seem to figure out if the use of std::bind is the way to go; and if it is, what I'm doing wrong yielding the following compiler error:
error: no matching function for call to 'bind'
std::bind(inject_foo, m, std::placeholders::_1));
^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:2953:1: note: candidate template ignored: couldn't infer template argument '_Fp'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:2962:1: note: candidate template ignored: couldn't infer template argument '_Rp'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Code I'm using:
#include <pybind11/numpy.h>
#include <boost/mp11.hpp>
#include <functional>
using boost::mp11::mp_product;
using boost::mp11::mp_for_each;
template <typename...> struct type_list {};
// all possible types
using my_type_list = type_list<
double, float, py::ssize_t, int, unsigned int, unsigned long>;
// construct all possible pairs of types with help from boost::mp11
using my_type_pairs = mp_product<
type_list, my_type_list, my_type_list>;
// the C++ function that we bind to a python module in the next function.
template <typename Tx, typename Ty>
py::array<py::ssize_t> foo(p::array_t<Tx> x, py::array_t<Ty>) {
py::array_t<py::ssize_t> z;
// do something with x and y
return z;
}
// bind foo<Tx, Ty> function to py::module m
template <typename Tx, typename Ty>
void inject_foo(py::module_& m, const type_list<Tx, Ty>&) {
m.def("_foo", &foo<Tx, Ty>, py::arg("x").noconvert(), py::arg("y").noconvert());
}
PYBIND11_MODULE(_backend, m) {
// these function calls work as expected:
// inject_foo(m, type_list<double, double>{});
// inject_foo(m, type_list<double, float>{});
// inject_foo(m, type_list<double, int>{});
// .....
// trying to make my life easier with the loop
// over all possible types of pairs is not working
mp_for_each(pg_type_pairs{}, std::bind(inject_foo, m, std::placeholders::_1));
}
PiotrNycz's comment led to an answer:
mp_for_each<pg_type_pairs>([&](const auto& x) { inject_foo(m, x); });
Provides the desired behavior (In the question I was even using the mp_for_each API incorrectly; switching to a lambda helped find that error).

Adding a type to an existing template with out it being dropped c++17

My goal is to be able to include my own extra type at declaration and have it passed to my template function. How would I be able to declare my type so that the compiler would not drop my extra template parameter.
For example I have this code:
#include <iostream>
#include <vector>
// my_vector is an alias for std::vector<T> that also takes an extra type E
template<typename T, typename E>
using my_vector = std::vector<T>;
// my aliased type is being demoted to std::vector<T, std::allocator<T> >
template<typename T, typename E>
void write_to(std::ostream stream, const my_vector<T, E>& vec) {
// I need E type for somthing here for example this
stream << static_cast<E>(vec.size());
for (auto elm : vec) {
stream << elm;
}
}
int main() {
// very redundantly declaring that I want my_vector
my_vector<float, uint8_t> vec = my_vector<float, uint8_t>{ 1.0f, 2.0f, 3.0f };
write_to(std::cout, vec);
// this would compile if I called write_to<uint8_t> but I want this to be assumed by the compiler
}
g++ output suggests that it is not passing my_vector<T, E> to write_to but instead drops my_vector altogether and instead passes std::vector<T, std::allocator<T> >, is it possible to get the compiler to not drop the extra template parameter so that I dont have to explicitly include it in every call of write_to here?
Here is my output from g++ std=c++17
[arkav:~/devel/packetize] $g++ template_demote.cc --std=c++17
template_demote.cc: In function ‘int main()’:
template_demote.cc:21:25: error: no matching function for call to ‘write_to(std::ostream&, my_vector<float, unsigned char>&)’
21 | write_to(std::cout, vec); // this would compile if I called write_to<uint8_t> but I want this to be assumed by the compiler
| ^
template_demote.cc:10:6: note: candidate: ‘template<class T, class E> void write_to(std::ostream, my_vector<T, E>&)’
10 | void write_to(std::ostream stream, const my_vector<T, E>& vec) {
| ^~~~~~~~
template_demote.cc:10:6: note: template argument deduction/substitution failed:
template_demote.cc:21:25: note: couldn’t deduce template parameter ‘E’
21 | write_to(std::cout, vec); // this would compile if I called write_to<uint8_t> but I want this to be assumed by the compiler
| ^
Solution
Inherent std::vector and its constructor in my type definition
template<typename E, typename T>
class my_vector: public std::vector {
using std::vector<T>::vector;
};
You can try something like this:
#include <iostream>
#include <vector>
template<class T, class E>
struct util {
std::vector<T> my_vector;
void write_to(std::ostream& stream) {
stream << static_cast<E>(my_vector.size());
for (auto elm : my_vector) {
stream << elm;
}
}
};
int main() {
util<float, uint8_t> u;
u.my_vector = std::vector<float>{ 1.0f, 2.0f, 3.0f };
u.write_to(std::cout);
}
You can read in cppreference that, when aliasing:
Alias templates are never deduced by template argument deduction when deducing a template template parameter.

Constraints not satisfied for template template concept requiring static template method

I'm trying to implement Functor and various other category-theoretic concepts using C++ concepts, but am getting compile errors:
http://coliru.stacked-crooked.com/a/e8b6eb387229bddf
Here's my full code (I know that requiring fmap<int, int> does not verify fmap for any two types, and I plan to change it to fmap<int, std::string> or something to achieve a slightly stronger test -- or instead, possibly alter the Functor concept so that it takes in addition to F, two types T and U and verifies the existence of fmap<T, U>, but that's all after I figure out how to fix the error that I'm getting):
#include <functional>
#include <iostream>
#include <vector>
// empty Functor_Impl struct - specialize for each functor
template<template<class> class F> struct Functor_Impl {};
// std::vector Functor implementation
template<>
struct Functor_Impl<std::vector> {
template<class T, class U>
static std::vector<U> fmap(std::vector<T> x, std::function<U(T)> f) {
std::vector<U> out;
out.reserve(x.size());
for (int i = 0; i < x.size(); i++) {
out.push_back(f(x[i]));
}
return out;
}
};
// Functor concept requires Functor_Impl<F> to have fmap
template<template<class> class F>
concept bool Functor = requires(F<int> x) {
{Functor_Impl<F>::template fmap<int, int>(x)} -> F<int>;
};
// Test function using constraint.
template<template<class> class F, class T>
requires Functor<F>
F<T> mult_by_2(F<T> a) {
return Functor_Impl<F>::template fmap<T, T>(a, [](T x) {
return x * 2;
});
}
int main() {
std::vector<int> x = {1, 2, 3};
std::vector<int> x2 = mult_by_2(x);
for (int i = 0; i < x2.size(); i++) {
std::cout << x2[i] << std::endl;
}
}
And the compile error:
lol#foldingmachinebox:~/p/website-editor$ g++ foo.cpp -std=c++17 -fconcepts -o foo
foo.cpp: In function ‘int main()’:
foo.cpp:39:38: error: cannot call function ‘F<T> mult_by_2(F<T>) [with F = std::vector; T = int]’
std::vector<int> x2 = mult_by_2(x);
^
foo.cpp:31:6: note: constraints not satisfied
F<T> mult_by_2(F<T> a) {
^~~~~~~~~
foo.cpp:24:14: note: within ‘template<template<class> class F> concept const bool Functor<F> [with F = std::vector]’
concept bool Functor = requires(F<int> x) {
^~~~~~~
foo.cpp:24:14: note: with ‘std::vector<int> x’
foo.cpp:24:14: note: the required expression ‘Functor_Impl<F>::fmap<int, int>(x)’ would be ill-formed
I'm guessing that my syntax for the concept itself is wrong - that it's treating a variable as a function, or vice versa, since I'm not very familiar with the concept syntax, and in addition some of the example code on cppreference.com does not compile under GCC's implementation (e.g. concept EqualityComparable does not compile, it must be changed to concept bool EqualityComparable).
If I remove requires Functor<F> from the mult_by_2 function declaration, then the code compiles and runs.
The problem is exactly what the error message says: Functor_Impl<F>::template fmap<int, int>(x) is not a valid expression. Functor_Impl<std::vector>::fmap has two parameters, not one.

Making interchangable forward/backwards for-range iterator

I wanted to be able to iterate collections (I will only we doing this on vectors, if that's important) in reverse order with simple for (const auto& item : collection) style, and I found this answer which worked perfectly: https://stackoverflow.com/a/28139075/2195721. But then I wanted to be able to iterate either forward or backward, so I modified the code as shown here:
#include <iterator>
#include <iostream>
#include <vector>
template <typename T>
struct reversable_wrapper {
T& iterable;
bool reverse;
reversable_wrapper(T&& iterable) : reversable_wrapper(iterable, true) {};
reversable_wrapper(T&& iterable, bool reverse) : iterable(iterable), reverse(reverse) {};
};
template <typename T>
auto std::begin (reversable_wrapper<T> w)
{
if (w.reverse) return std::rbegin(w.iterable);
else return std::begin(w.iterable);
}
template <typename T>
auto std::end (reversable_wrapper<T> w)
{
if (w.reverse) return std::rend(w.iterable);
else return std::end(w.iterable);
}
template <typename T>
reversable_wrapper<T> reverse (T&& iterable) { return reversable_wrapper<T>(iterable); }
template <typename T>
reversable_wrapper<T> forward (T&& iterable) { return reversable_wrapper<T>(iterable, false); }
int main()
{
std::vector<int> vec = {1,2,3};
for (const auto& i : reverse(vec)) std::cout<<i<<std::endl;
return 0;
}
Basically, what I wanted to achieve was for (const auto& item : cond ? forward(coll) : reverse(coll)). However, this code gives me following errors:
> $ g++ -std=c++14 iterators.cpp -o iterators
iterators.cpp: In instantiation of ‘auto std::begin(reversable_wrapper<T>) [with T = std::vector<int>&]’:
iterators.cpp:37:37: required from here
iterators.cpp:18:38: error: inconsistent deduction for ‘auto’: ‘std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >’ and then ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’
else return std::begin(w.iterable);
^
iterators.cpp: In instantiation of ‘auto std::end(reversable_wrapper<T>) [with T = std::vector<int>&]’:
iterators.cpp:37:37: required from here
iterators.cpp:25:36: error: inconsistent deduction for ‘auto’: ‘std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >’ and then ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’
else return std::end(w.iterable);
I can't figure which signature can I give to std::begin and std::end to overcome this and if this is possible at all.
I'm using g++ 5.4.0.
Your problem is that you are trying to return one of std::vector::iterator or std::vector::reverse_iterator from your functions. Sadly, these are unrelated types.
I think your only chance is to write a reversible_iterator_wrapper which holds both types, and forwards its arguments to the right one. This is going to be tedious. The simplest (but not most efficient) option probably involves virtual functions!