I was trying using Boost Phoenix. My aim is to have stl algorithms that take a container instead of range of iterators, as described
here.
However, I am getting a mess of errors on a rather simple code:
#include <boost/phoenix/stl/algorithm.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
namespace phx = boost::phoenix;
int main(int argc, char ** argv) {
std::vector<int> my_arr{ 0, 1, 2, 3 };
phx::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}
I tried it on two platforms (Win7, Ubuntu) where I otherwise use multiple parts of Boost.
The error messages are rather long so I put them in files:
MVC++ November 2013, Boost 1.55
G++4.7.2, Boost 1.53
I can't really make much of the errors apart from templates not being compiled correctly, but I guess I am missing something rather simple.
I think you are using the wrong boost library. The phoenix algorithms are lazy functions which are explained here. So phoenix::for_each does not do anything if it is called, but returns a function object which iterates over the range once it is called. If you simply want to use the STL (and other) algorithms on ranges you can use the boost.range library:
#include <boost/range/algorithm/for_each.hpp>
#include <iostream>
#include <vector>
namespace rng = boost::range;
int main(int argc, char ** argv) {
std::vector<int> my_arr{ 0, 1, 2, 3 };
rng::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}
You simply need to include the phoenix core before including anything else.
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
... rest of your program
Related
I have a question which is related to the question (and answers) available over here:
C++ Boost Interval and cos
The solution presented in the linked above works for me for most trigonometrical functions including the hyperbolic once. However, if I try to use the invers of this hyperbolic once, let's take asinh() as an example I get the following compiler error:
error C2784: "boost::numeric::interval<T,Policies>
boost::numeric::asinh(const boost::numeric::interval<T,Policies> &)":
could not deduce template argument for "const
boost::numeric::interval<T,Policies> &" from "const double"
A code that produces the error is
#include "stdafx.h"
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>
using namespace std;
using namespace boost::numeric::interval_lib;
using namespace boost::numeric;
typedef interval<double, policies<save_state<rounded_transc_std<double> >,
checking_base<double> > > Interval;
int _tmain(int argc, _TCHAR* argv[])
{
Interval test = asinh(Interval(1, 1.1));
return 0;
}
I am using the boost 1_65_1 libary. How to get the invers of the hyperbolic functions running?
Of cause one workaround would be to use the identity
Interval test = log(Interval(1, 1.1) + sqrt(pow(Interval(1, 1.1),2)+1.));
This works perfectly fine and produces correct results. However, it must be possible to use the implemented asinh-function directly.
There might be a MSVC-related bug, because it works for me using GCC or clang:
Live On Coliru (GCC c++03)
Live On Coliru (Clang c++03)
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>
#include <iostream>
namespace bn = boost::numeric;
namespace bni = bn::interval_lib;
typedef bn::interval<double, bni::policies<bni::save_state<bni::rounded_transc_std<double> >,
bni::checking_base<double> > > Interval;
int main()
{
Interval test = asinh(Interval(1, 1.1));
std::cout << test;
}
Prints
[0.881374,0.950347]
I'm tring to use boost array
but I got this error:
error: 'array': ambiguous symbol
here is my code:
#include <iostream>
#include <boost/array.hpp>
#include <boost/regex.hpp>
using namespace boost;
using namespace std;
int main(int argc, char* argv[])
{
array<int, 10> a{3};
cout << "a[0]= " << a[0];
return 0;
}
This error arise when I include the boost library
any idea ?
boost::array and std::array are not ambiguous because they are scoped by different namespaces.
When you do:
using namespace boost;
using namespace std;
You tell the compiler to look symbols up in both these namespaces. But the compiler now doesn't know which array you're talking about when you just type array. It could be boost::array or it could be std::array thus it is ambiguous.
You could fix this by either removing the using namespace std; or by specifying that you're using the Boost version by using boost::array instead of just array.
Incidentally I understand trying to learn Boost functionality, but you shouldn't bother learning boost::array, right in the Introduction to boost::array it tells you:
std::array is (as of C++11) part of the C++ standard. The differences between boost::array and std::array are minimal. If you are using C++11, you should consider using std::array instead of boost::array.
add core:: before the array
so core::array
I am experimenting with proto and phoenix and what is one of my first toy examples crash and I have no idea where I should be looking at. Since someone on the #boost IRC channel told me to ensure that the phoenix expression tree is first deep copied (so that there are no dangling references left when x has been constructed), I wrapped the expression by boost::proto::deep_copy. However that didn't quite work. It still crashes when compiled with the -O2 flag, and works fine when omitting it.
#include <boost/phoenix/phoenix.hpp>
#include <boost/proto/deep_copy.hpp>
#include <iostream>
namespace bpr = boost::proto;
int main(int argc, char **argv) {
using namespace boost::phoenix;
using namespace placeholders;
auto x = bpr::deep_copy(
switch_(arg1)[
case_<1>(std::cout << val("hello")),
case_<2>(std::cout << val("bye")),
default_(std::cout << val("default"))
]);
x(1);
x(2);
}
I expect this to output hellobye.
Looks like this is a known bug in Phoenix. I would avoid using phoenix::switch_ until this is sorted. Unfortunately, the maintainer of Phoenix seems to be busy with other things these days. :-(
I am trying to compile the following code:
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/assign/std/vector.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/copy.hpp>
int main( int argc, char ** argv )
{
using namespace boost::assign;
using boost::adaptors::indirected;
std::vector<boost::optional<unsigned> > values;
values += 1u,2u,3u;
boost::copy( values | indirected, std::ostream_iterator<unsigned>( std::cout, " " ) );
std::cout << std::endl;
}
However, I got some errors, e.g. that there is no type named element_type in boost::optional<unsigned>. The reference page page, however, says that the single precondition is the existence of the operator*() unary function. Is there a way to make it work?
This is definitely a bug in Boost, but whether that bug is in Boost.Optional or Boost.Iterator is up for debate (I would say the latter, personally).
However, the fix is trivial -- before including any Boost headers, do this:
#include <boost/optional/optional_fwd.hpp>
#include <boost/pointee.hpp>
namespace boost
{
template<typename P>
struct pointee<optional<P> >
{
typedef typename optional<P>::value_type type;
};
}
Then include other Boost headers as necessary.
Please submit a ticket on the Boost Trac, or at the least post a bug report on the Boost Users mailing list.
Look at the private optional.hpp defined in boost iostreams library here. You will see that it defines a typedef T element_type;
However the actual optional.hpp that you are using defined here does not define it. So that is why the compiler is complaining. I don't know why it was overlooked.
Try using the private optional.hpp from iostreams library to solve this issue. I hope this helps.
I'm trying to start using namespaces the correct (or at least best) way.
The first thing I tried to do was to avoid putting using namespace xxx; at the beginning of my files. Instead, I want to using xxx::yyy as locally as possible.
Here is a small program illustrating this :
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
using std::cout;
using std::endl;
srand(time(0));
for(int i=0; i<10;++i)
cout << rand() % 100 << endl;
return 0;
}
If I omit the lines using std::cout; or using std::endl, the compiler will complain when I'm trying to use cout or endl.
But why is this not needed for srand, rand and time ? I'm pretty sure they are in std, because if I try to specifically pour std:: in front of them, my code is working fine.
If you use cstdlib et al. the names in them are placed in both the global and the std:: namespaces, so you can choose to prefix them with std:: or not. This is seen as a feature by some, and as a misfeature by others.
If you really want to know, take a close look at the ctime and cstdlib headers. They were built backwards-compatible.
Note: all this using vs. using namespace business is about readability. If your IDE would allow to just not show the namespaces when you don't want to see them, you wouldn't need these constructs...
I prefer to omit using and just have the std::cout every time just to maintain readability. although this is probably only useful in larger projects
As long as we on the subject, there's also a thing called Koenig Lookup which allows you to omit a namespace identifier before a function name if the arguments it take come from the same namespace.
For example
#include <iostream>
#include <algorithm>
#include <vector>
void f(int i){std::cout << i << " ";}
int main(int argc, char** argv)
{
std::vector<int> t;
// for_each is in the std namespace but there's no *std::* before *for_each*
for_each(t.begin(), t.end(), f);
return 0;
}
Well, it's not related directly but I though it may be useful.