Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"nice" keyword in c++?

Tags:

c++

So I was doing some simple C++ exercises and I noticed an interesting feat. Boiled down to bare metal one could try out compiling the following code:

class nice
{
  public:
    nice() {}
};

int main()
{
  nice n;
  return 0;
};

The result is a compilation error that goes something like this:

<file>.cpp: In function ‘int main()’:
<file>.cpp:11: error: expected `;' before ‘n’
<file>.cpp:11: warning: statement is a reference, not call, to function ‘nice’
<file>.cpp:11: warning: statement has no effect

And this was using regular g++ on Max OS X, some of my friends have tried in on Ubuntu as well, yielding the same result.

The feat seems to lie in the word "nice", because refactoring it allows us to compile. Now, I can't find the "nice" in the keyword listings for C++ or C, so I was wondering if anyone here had an idea?

Also, putting

class nice n;

instead of

nice n;

fixes the problem.

P.S. I'm a relative C++ newbie, and come from the ActionScript/.NET/Java/Python world.

Update:

Right, my bad, I also had an

#include <iostream>

at the top, which seems to be the root of the problem, because without it everything works just fine.

like image 731
oleks Avatar asked Feb 13 '10 23:02

oleks


3 Answers

Maybe the problem is somehow caused by function nice in libc. It is similar to trying to name your class printf.

like image 58
Messa Avatar answered Nov 15 '22 08:11

Messa


using namespace std, by any chance?

Edit:

The standard says that standard headers define all their symbols in namespace std (see 17.4.1.2.4). A footnote, however, also says that the <.h> variants dump their names into the global namespace - but of course no one should be using these ;)

like image 26
James Avatar answered Nov 15 '22 07:11

James


It is a namespace problem but not with namespace std. The header <iostream> is pulling in <unistd.h>

If you try

class nice
{
  public:
      nice() {};
};

int main(int argc, char *argv[])
{
    nice n;

    return 0;
}

there is no problem.

Simply add

#include <unistd.h>

and you will get the "expected ‘;’ before ‘n’" error. Namespace std does not enter the picture.

So the solution is the same as before - put class nice in its own namespace and it will not clash with the global ::nice().

like image 38
Duck Avatar answered Nov 15 '22 08:11

Duck