1

Had following type:

typedef pair<double, double> MinMax; ///< first - Min, second - Max

and initialization with it worked fine:

const MinMax mInMinMax[FunctionCount] = {{-1, 1}, {-1, 1}, {0, 1}};

However if I subclass pair for convenience:

///< first - Min, second - Max
struct MinMax : public pair<double, double>
{
    double& Min() { return first; }
    double Min() const { return first; }
    double& Max() { return second; }
    double Max() const { return second; }
};

Compilation fails with error:

error: could not convert ‘{-1, 1}’ from ‘<brace-enclosed initializer list>’ to ‘const MinMax’ const MinMax mInMinMax[FunctionCount] = {{-1, 1}, {-1, 1}, {0, 1}};

Is it possible to subclass pair<double, double> correctly?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101
  • 1
    In general it's not a good idea to subclass STL containers: http://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers/7110262#7110262 – Andy May 16 '17 at 18:50

1 Answers1

3

The constructor of the base class is not "inherited" automatically. Your Min/Max functions have nothing to do with this problem:

struct MinMax : public pair<double, double>
{
   using pair<double, double>::pair;
   ...
};
alfC
  • 14,261
  • 4
  • 67
  • 118