'const' ignored when compiling in linux

N

none

This:

#include <vector>
class Image2D {
public:
void test() const {
con.push_back(234);
}
private:
std::vector<int> con;
};

int main(){

Image2D img;
// test();
return 0;

}

compiles fine on ubuntu linux using gcc 4.4.1. But I get an error when I try to compile the same
code in Visual studio 2008. I can fix this error in VS if I declare:

mutable std::vector<int> con;

But why does the gcc compiler not complain when I modify a member in a const function?
 
N

none

Christian said:
none ha scritto:


It should not. Are you sure this is exactly the same code that you
actually compiled? If it really is the same, then you must be invoking
the compiler with some wrong options.

These are the options that I use:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -s -lX11 -Dcimg_use_xshm -lXext -Dcimg_use_xrandr
-lXrandr -O3 -fpermissive -march=nocona -m64 -ffast-math -funroll-loops -finline-functions")

There are plenty of other stuff that is not caught using gcc. As an example I have this:

template <typename A, typename J>
class Producer {
public:
typedef J JobType;
const unsigned int Dimension = JobType::Dimension;

...
....

which compiles fine when using gcc. But in VS I need to a the static keyword to make it compile:

const static unsigned int Dimension = JobType::Dimension;
 
N

Niels Dekker - no reply address

none" <""mort\"@(none) said:
These are the options that I use:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -s -lX11 -Dcimg_use_xshm
-lXext -Dcimg_use_xrandr -lXrandr -O3 -fpermissive -march=nocona -m64
-ffast-math -funroll-loops -finline-functions")
There are plenty of other stuff that is not caught using gcc. As an
example I have this:

Why do you use "-fpermissive"?

http://gcc.gnu.org/onlinedocs/gcc-4...lect-Options.html#C_002b_002b-Dialect-Options
says:

"-fpermissive will allow some nonconforming code to compile."

HTH,

Niels
 
A

Alf P. Steinbach

* none, on 19.05.2010 19:33:
These are the options that I use:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -s -lX11 -Dcimg_use_xshm
-lXext -Dcimg_use_xrandr -lXrandr -O3 -fpermissive -march=nocona -m64
-ffast-math -funroll-loops -finline-functions")

Don't know, the "permissive" thing seems ungood, but I don't know what it does.
I suggest check it out.

It really shouldn't compile.

Are you sure that that's exactly the code you're trying to compile? E.g.,
templating can make things compile when they're not actually used. And that's a
feature, not a bug... :)

There are plenty of other stuff that is not caught using gcc. As an
example I have this:

template <typename A, typename J>
class Producer {
public:
typedef J JobType;
const unsigned int Dimension = JobType::Dimension;

..
...

which compiles fine when using gcc. But in VS I need to a the static
keyword to make it compile:

const static unsigned int Dimension = JobType::Dimension;

That sounds like a C++0x thing in g++.

Add options '-std=c++98 -pedantic -ansi'.

Or you may have to adjust, I'm just writing this from memory.


Cheers & hth.,

- Alf (still practical mode)
 
V

Vladimir Jovic

none said:
Arg I better turn it back on, its exploding with errors ! - maybe the
original authors of the code had a reason for adding this option. But
thanks for the tip!


Better would be to use correct compiling options, and fix all errors.
 
J

Juha Nieminen

none said:
Arg I better turn it back on, its exploding with errors ! - maybe the original authors of the code
had a reason for adding this option.

Yes: Laziness.
 
J

Jorgen Grahn

.
(gcc)
Add options '-std=c++98 -pedantic -ansi'.

Or you may have to adjust, I'm just writing this from memory.

Close.

g++ -std=c++98 -pedantic -Wextra -Wall -O3

is my standard set of warning options. (Yes, enabling optimization
gives you some more warnings).

/Jorgen
 
J

Juha Nieminen

Jorgen Grahn said:
(Yes, enabling optimization gives you some more warnings).

I have heard that before. It would be interesting to see an example of a warning which is shown with optimizations but not without them.
 
A

Alf P. Steinbach

* Juha Nieminen, on 22.05.2010 07:58:
I have heard that before. It would be interesting to see an example of a
warning which is shown with optimizations but not without them.

With g++ the main example is use of an uninitialized variable.



Cheers & hth,.

- Alf
 
J

Jorgen Grahn

* Juha Nieminen, on 22.05.2010 07:58:

With g++ the main example is use of an uninitialized variable.

Other examples are unused parameters, and unused translation-unit-
local functions. IIRC you have to use -O3 rather than -O2 to see some
of those.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top