> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:nlopt-discuss-
> [email protected]] Im Auftrag von Florian Dommert
> Gesendet: Montag, 24. Juni 2013 19:29
> An: 'Steven G. Johnson'
> Cc: [email protected]
> Betreff: Re: [NLopt-discuss] unsigned vs size_t in add_constraints
>
> > -----Ursprüngliche Nachricht-----
> > Von: Steven G. Johnson [mailto:[email protected]]
> > Gesendet: Montag, 24. Juni 2013 19:17
> > An: Florian Dommert
> > Cc: [email protected]
> > Betreff: Re: [NLopt-discuss] unsigned vs size_t in add_constraints
> >
> >
> > On Jun 24, 2013, at 1:13 PM, "Florian Dommert" <[email protected]
> > stuttgart.de> wrote:
> > > Hi,
> > > this is surely true, but warnings are annoying in a nice clean code.
> > > Are you interested in a patch? Perhaps I can work one out without
> > > affecting the general behavior.
> >
> >
> > Sure. The simplest option would be to insert a few typecasts into
> nlopt.hpp.
> > Another option would be to change the C interface to use size_t, but
> > I'm reluctant to change the ABI without a stronger motivation.
>
> Fine. In my own opinion, the first option is really the simplest. I will
give it a try
> and send you a patch.
>
Hello,
attached is a patch for nlopt.hpp. A public member function convertSize_t
is introduced that casts the size into an unsigned and checks, if the
conversion is correct. Otherwise an exception is thrown.
However, this is just a first step and only avoids compiler warnings when
linking to nlopt. There are some similar constructs in the nlopt code where
a size_t is used as an unsigned. These warnings are thrown during the
compilation process of nlopt. If I have more time left and you consider the
attached fix as appropriate, then I can also include it at the other
sections of the code.
Cheers,
Flo
> Cheers,
> Flo
>
>
> _______________________________________________
> NLopt-discuss mailing list
> [email protected]
> http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
--- api\nlopt.hpp 2012-07-20 21:47:07 +0200
+++ my_api\nlopt.hpp 2013-06-25 14:26:03 +0200
@@ -128,6 +128,8 @@
forced_stop() : std::runtime_error("nlopt forced stop") {}
};
+
+
//////////////////////////////////////////////////////////////////////
class opt {
@@ -317,6 +319,18 @@
result last_optimize_result() const { return last_result; }
double last_optimum_value() const { return last_optf; }
+ unsigned convertSize_t(size_t invalue) {
+ try {
+ if(static_cast<unsigned>(invalue)!=invalue)
+ throw std::runtime_error("too many
constraints");
+ else
+ return static_cast<unsigned>(invalue);
+ }
+ catch(...){
+ return 0;
+ }
+ }
+
// accessors:
algorithm get_algorithm() const {
if (!o) throw std::runtime_error("uninitialized nlopt::opt");
@@ -409,7 +423,9 @@
if (!d) throw std::bad_alloc();
d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
d->munge_destroy = d->munge_copy = NULL;
- mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d,
+ unsigned tolsize=convertSize_t(tol.size());
+
+ mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}
@@ -438,7 +454,9 @@
if (!d) throw std::bad_alloc();
d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
d->munge_destroy = d->munge_copy = NULL;
- mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d,
+ unsigned tolsize=convertSize_t(tol.size());
+
+ mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}
@@ -468,7 +486,8 @@
if (!d) throw std::bad_alloc();
d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
d->munge_destroy = md; d->munge_copy = mc;
- mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d,
+ unsigned tolsize=convertSize_t(tol.size());
+ mythrow(nlopt_add_inequality_mconstraint(o, tolsize, mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}
void add_equality_mconstraint(mfunc mf, void *f_data,
@@ -478,7 +497,8 @@
if (!d) throw std::bad_alloc();
d->o = this; d->mf = mf; d->f_data = f_data; d->f = NULL; d->vf = NULL;
d->munge_destroy = md; d->munge_copy = mc;
- mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d,
+ unsigned tolsize=convertSize_t(tol.size());
+ mythrow(nlopt_add_equality_mconstraint(o, tolsize, mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss