mercredi 30 mai 2018

Can I use static const float in header with value initialization

I have this question bugging me.

Why cant I have code like this in my .hpp file.

class H {
  private:
    static const int i =5;
    static const float f = 1.0;
    static const string s = "string";
}

Only int, bool, enum and constexpr etc can be declared and initialized like that.

My questions:

  1. Why do strings and other complex datatypes needs proper initialization in cpp? They are constant.

  2. Why do we have different behavior for floats and ints? my guess:gcc does not support that, but can be supported easily if we use constexpr.

Aucun commentaire:

Enregistrer un commentaire