The long code is below, here is the relevant part and why I think the compiler does not like it.
I have a static member function in class t_threads as so-
Expand|Select|Wrap|Line Numbers
- static void run()
- {
- for( set<t_thread>::iterator ix = _s_pobj->_threads.begin(); ix != _s_pobj->_threads.end(); ++ix )
- (*ix).run();
- }
The error from g++ is -
w.cpp: In static member function `static void t_threads::run()':
w.cpp:95: error: passing `const t_thread' as `this' argument of `void t_thread::run()' discards qualifiers
This should be allowed, IMO. I need the t_threads::run function to be static because of the way I call it in main(). See below. Of course I could change the code but this is just a small part of a much larger system and I have to change similar constructs in numerous places and I am going to stick with MS C++ compiler v15 for the time being.
The sample code-
Expand|Select|Wrap|Line Numbers
- #include <stdlib.h>
- #include <assert.h>
- #include <iostream>
- #include <string>
- #include <set>
- using namespace std;
- // the t_thread object
- class t_thread
- {
- public:
- t_thread( int _tid )
- : tid_ (_tid), run_(false)
- {}
- void run() { run_ = true; cout << "run(" << tid_ << ") running!\n"; }
- void dump() const { cout << "dump(" << tid_ << ") run=" << run_ << "\n"; }
- bool operator<( const t_thread& t2 ) const {
- return tid_ < t2.tid_;
- }
- private:
- bool run_;
- int tid_;
- };
- // the t_threads object
- class t_threads
- {
- public:
- static int initialize( string& serr )
- {
- // check for multiple initialization
- if( _s_initialized ) {
- serr = "t_threads: multiple initialization not allowed!";
- return -1;
- }
- // create the global t_threads object
- _s_pobj = new t_threads();
- if( !_s_pobj ) {
- serr = "t_threads: cannot create new t_threads!";
- return -1;
- }
- // remember initialization
- _s_initialized = true;
- return 0;
- }
- static void finalize()
- {
- delete _s_pobj;
- _s_initialized = false;
- }
- static const t_threads& _this()
- {
- return *_s_pobj;
- }
- static void dump()
- {
- for( set<t_thread>::const_iterator ixc = _s_pobj->_threads.begin(); ixc != _s_pobj->_threads.end(); ++ixc )
- (*ixc).dump();
- }
- static const set<t_thread>& threads()
- {
- return _s_pobj->_threads;
- }
- static int create( int _tid )
- {
- pair<set<t_thread>::iterator,bool> pair_ = _s_pobj->_threads.insert( t_thread( _tid ) );
- assert( pair_.second );
- return 0;
- }
- static void run()
- {
- for( set<t_thread>::iterator ix = _s_pobj->_threads.begin(); ix != _s_pobj->_threads.end(); ++ix )
- (*ix).run();
- }
- private:
- t_threads() {}
- t_threads( const t_threads& );
- t_threads& operator=( const t_threads& );
- set<t_thread> _threads;
- static t_threads* _s_pobj;
- static bool _s_initialized;
- };
- t_threads* t_threads::_s_pobj = NULL;
- bool t_threads::_s_initialized = false;
- // main section
- int main(int argc, char **argv)
- {
- string serr;
- if( -1 == t_threads::initialize(serr) ) {
- cout << "main: t_threads::initialize error " << serr;
- return -1;
- }
- for( int i = 0; i < atoi(argv[1]); i++ )
- if( -1 == t_threads::create(i)) {
- cout << "main: t_threads::create error " << serr;
- return -1;
- }
- t_threads::dump();
- t_threads::run();
- t_threads::dump();
- t_threads::finalize();
- return 0;
- }