Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22031

overflow in implicit constant conversion [-Werror=overflow] in class member initializer only on one constructor

$
0
0

There has been several similar questions, but this one could(?) be due to a bug in the compiler of gcc. The only const is the parameter vvalue. I have changed to char* and still got the same compiler warning.

This is a third party code with class (not showing namespace etc. Only showing relevant part

class Value {
public:
    ....
    Value(double value);
    Value(const char *vvalue);
    ....
private:
        struct CommentInfo {
          CommentInfo();
           ~CommentInfo();
          void setComment( const char *text );
           char *comment_;
      };

        union ValueHolder {
          Int int_;
           UInt uint_;
           double real_;
           bool bool_;
           char *string_;
 #ifdef JSON_VALUE_USE_INTERNAL_MAP
           ValueInternalArray *array_;
          ValueInternalMap *map_;
 #else
         ObjectValues *map_;
 #endif
        } value_;
        ValueType type_ : 8;
        int allocated_ : 1;     // Notes: if declared as bool, bitfield is useless.
 #ifdef JSON_VALUE_USE_INTERNAL_MAP
       unsigned int itemIsUsed_ : 1;      // used by the ValueInternalMap container.
        int memberNameIsStatic_ : 1;       // used by the ValueInternalMap container.
 #endif
        CommentInfo *comments_;
     };

};

/// class definition
 316 Value::Value( double value )
 317    : type_( realValue )
 318    , comments_(nullptr)
 319 # ifdef JSON_VALUE_USE_INTERNAL_MAP
 320    , itemIsUsed_( 0 )
 321 #endif
 322 {
 323    value_.real_ = value;
 324 }
 325 
 326 Value::Value(const char *vvalue)
 327    : type_(stringValue), allocated_(true),
 328       comments_(nullptr)
 329 #ifdef JSON_VALUE_USE_INTERNAL_MAP
 330       , itemIsUsed_( 0 )
 331 #endif 
 332 {     
 333    value_.string_ = duplicateStringValue(vvalue);
 334 }     

When we run with -Werror -Wall we get the following error message only with the constructor taking inputs but not other constructors for the comments_ member:

json_value.cpp:328:24: error: overflow in implicit constant conversion [-Werror=overflow]
       comments_(nullptr)
                        ^

Strange enough the warning does not occur on the constructor with (double value) version. I have stared at the code for a long time and don't see where I can change to eliminate this warning.

This may have something to do with the bit field:

 309 Value::Value( double value )
 310    : type_( realValue ),
 311    //allocated_(true), // adding this caused overflow issue
 312    //allocated_(1), // adding this caused overflow issue
 313    //allocated_(0), // this eliminates overflow issue
 314    allocated_(false), // this eliminates overflow issue
 315    comments_(nullptr)

What I found, the warning has something to do with the bit field in previous class member: alocated_ which is a 1 bit for boolean. Not sure the code is a bad practice or what.


Viewing all articles
Browse latest Browse all 22031

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>