Why does this proto/phoenix toy example crash? - c++

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. :-(

Related

(Software issue) Can anyone suggest a download that will definitely bring my Mingw32 compiler up to speed?

#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?

Boost Phoenix compilation error

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

C++ Program Outputting Wrong Number

This is my program:
#include "stdafx.h"
#include <iostream>
using namespace std;
double areaofcircle(double r)
{
return 3.14 * r * r;
}
int main()
{
cout << areaofcircle(5);
}
I should be getting the output of "78.5" but I keep getting "78.512". What is going on?!
I've also tried float, but I still get the same output.
Also just a side question, do I really need to add "return 0;" to the main function?
One more side question, do I need to write "using namespace std;" inside every function, or can I just write it outside of everything, like how I've been doing it.
You're passing the literal for an integer (5) so somewhere an implicit conversion is required to turn it into a double. You would be better off passing 5.0. The C++ default for doubles requires no specifier so your 3.14 is fine. (specifying a float requires 3.14f). That said, I tried both with 5 and 5.0 and got 78.5 both times on my compiler.
How you're using the std namespace is fine, but as pointed out it does bring ALL of the standard namespace into scope. I see this a lot in teaching material. It is better to just use
using std::cout;
or just explicitly add std::cout to all uses. However, there is nothing "wrong" from a compilation standpoint in the way you did it.
I think you are doing something wrong. I tried the same on GCC compiler, and I do get 78.5. Which compiler are you using?
Regarding your other questions
It is always a good idea to return the state of your program from main. Usually, you can return EXIT_SUCCESS if everything works okay, else you can return EXIT_FAILURE.
No it is not necessary to include using namespace std. Instead, it is bad practice to pollute your standard namespace. You should include only those functions that you use very frequently.
To read more about C++. Check this link
Hope this helps.
Tried a few experiments on VS 2008 to see if I could get a similar error. By changing pi to a float I do get 78.500002622604370 which is different but not the same as your issue. But I do get 78.5 when pi is a double.
I'd recommend you let us know which compiler and version you're using, then possibly someone may be able to help.
#include "stdafx.h"
#include <iostream>
const double pi = 3.14;
double areaofcircle(double r)
{
return pi * r * r;
}
int _tmain(int argc, _TCHAR* argv[])
{
double temp = areaofcircle(5);
std::cout << temp;
return 0;
}

Using Namespace std

I am taking a programming class in school and I wanted to start doing some c++ programming out of class. My school using Microsoft Visual C++ 6.0 (which is from 1998) so it still uses <iostream.h> rather than <iostream> and using namespace std. When I started working, I couldn't figure out how and when to use using namespace std and when to just use things like std::cout<<"Hello World!"<<'\n'; (for example) as well as it's limits and other uses for the namespace keyword. In particular, if I want to make a program with iostream and iomanip, do I have to state "using namespace std" twice, or is there something different that I would have to use as well, or can I just do the same thing as I did with iostream? I tried googling it but I didn't really understand anything. Thanks in advance for the help.
Ok, handful of things there, but it is manageable.
First off, the difference between:
using namespace std;
...
cout << "Something" << endl;
And using
std::cout << "Something" << std::endl;
Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type using namespace std, you are taking everything inside of the namespace std and moving it to the global scope, so that you can use the shorter cout instead of the more fully-qualified std::cout.
One thing to understand about namespaces is that they stretch across files. Both <iostream> and <iomanip> use the namespace std. Therefore, if you include both, then the declaration of using namespace std will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).
There are going to be people who tell you "don't use using namespace std!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int cout = 0;
int endl = 1;
cout << cout << endl << endl; // The compiler WILL freak out at this :)
return 0;
}
When I use using namespace std, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.
I can write the same program as before (but get it to work) by not using the statement using namespace std:
#include <iostream>
int main(int argc, char** argv) {
int cout = 0;
int endl = 1;
std::cout << cout << endl << std::endl; // Compiler is happy, so I'm happy :)
return 0;
}
Hopefully this has clarified a few things.
If you use the header names without the .h, then the stuff declared/defined in it will be in the std namespace. You only have to use using namespace std; once in the scope where you want stuff imported in order to get everything; more than one using namespace std; doesn't help anything.
I'd recommend against using namespace std; in general, though. I prefer to say, for example, using std::cout; instead, in order to keep names in std from conflicting with mine.
For example:
#include <iostream>
#include <iomanip>
int main()
{
using namespace std;
int left = 1, right = 2;
cout << left << " to " << right << "\n";
}
may cause mysterious issues, because left and right exist in the std namespace (as IO manipulators), and they get imported if you lazily say using namespace std;. If you meant to actually use the IO manipulators rather than output the variables, you may be a bit disappointed. But the intent isn't obvious either way. Maybe you just forgot you have ints named left and right.
Instead, if you say
#include <iostream>
#include <iomanip>
int main()
{
using std::cout;
int left = 1, right = 2;
cout << left << " to " << right << "\n";
}
or
#include <iostream>
#include <iomanip>
int main()
{
int left = 1, right = 2;
std::cout << left << " to " << right << "\n";
}
everything works as expected. Plus, you get to see what you're actually using (which, in this case, includes nothing from <iomanip>), so it's easier to keep your includes trimmed down to just what you need.
Here is a good link that describes namespaces and how they work.
Both methods are correct, that is, you can either introduce a namespace with the "using" statement or you can qualify all the members of the namespace. Its a matter of coding style. I prefer qualifying with namespaces because it makes it clear to the reader in which namespace the function / class is defined.
Also, you do not have to introduce a namespace twice if you are including multiple files. One using statement is enough.
Good question, Ryan. What using namespace does is importing all symbols from a given namespace (scope) into the scope where it was used. For example, you can do the following:
namespace A {
struct foo {};
}
namespace B {
using namespace A;
struct bar : foo {};
}
In the above examples, all symbols in namespace A become visible in namespace B, like they were declared there.
This import has affect only for a given translation unit. So, for example, when in your implementation file (i.e. .cpp) you do using namespace std;, you basically import all symbols from std namespace into a global scope.
You can also import certain symbols rather than everything, for example:
using std::cout;
using std::endl;
You can do that in global scope, namespace scope or function scope, like this:
int main ()
{
using namespace std;
}
It is up to a programmer to decide when to use fully qualified names and when to use using keyword. Usually, it is a very bad taste to put using into a header files. Professional C++ programmers almost never do that, unless that is necessary to work around some issue or they are 100% sure it will not mess up type resolution for whoever use that header.
Inside the source file, however (nobody includes source files), it is OK to do any sort of using statements as long as there are no conflicting names in different namespaces. It is only a matter of taste. For example, if there are tons of symbols from different namespaces being used all over the code, I'd prefer at least some hints as for where they are actully declared. But everyone is familiar with STL, so using namespace std; should never do any harm.
There also could be some long namespaces, and namespace aliasing comes handy in those cases. For example, there is a Boost.Filesystem library that puts all of its symbols in boost::filesystem namespace. Using that namespace would be too much, so people usually do something like this:
namespace fs = boost::filesystem;
fs::foo ();
fs::bar ();
Also, it is almost OK to use namespace aliasing in headers, like this:
namespace MyLib {
namespace fs = boost::filesystem;
}
.. and benefit from less typing. What happens is that users that will use this header, will not import the whole filesystem library by saying using namespace MyLib;. But then, they will import "fs" namespace from your library that could conflict with something else. So it is better not to do it, but if you want it too badly, it is better than saying using namespace boost::filesystem there.
So getting back to your question. If you write a library using C++ I/O streams, it is better not to have any using statements in headers, and I'd go with using namespace std; in every cpp file. For example:
somefile.hpp:
namespace mylib {
class myfile : public std::fstream {
public:
myfile (const char *path);
// ...
};
}
somefile.cpp:
#include "somefile.hpp"
using namespace std;
using namespace mylib;
myfile::myfile (const char *path) : fstream (path)
{
// ...
}
Specific to using namespace std
You really shouldn't ever use it in a header file. By doing so, you've imported the entire 'std' into the global namespace for anyone who includes your header file, or for anyone else that includes a file that includes your file.
Using it inside a .cpp file, that's personal preference. I typically do not import the entire std into the global namespace, but there doesn't appear to be any harm in doing it yourself, to save a bit of typing.

namespace usage

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.