[constrained_value] need help with runtime error

This code has an error: #include <boost/numeric/ublas/vector.hpp> #include <boost/constrained_value.hpp> namespace cv = boost::constrained_value; namespace ublas = boost::numeric::ublas; typedef cv::bounded<int>::type val_t; typedef cv::bounded<int>::type::constraint_type constraint_type; int main() { int min = -200; int max = 200; val_t init (10, constraint_type (min, max)); ublas::vector<val_t> v (10, init); ublas::vector<val_t> w = -v; } This code will throw an exception. The last line invokes: ublas/functional.hpp:70 (scalar_negate), which for some reason calls constrained.hpp:224, which is: constrained(const value_type & v) That is, initialize only the value, not the constraint! Any ideas?

AMDG Neal Becker wrote:
This code has an error:
#include <boost/numeric/ublas/vector.hpp> #include <boost/constrained_value.hpp>
namespace cv = boost::constrained_value; namespace ublas = boost::numeric::ublas;
typedef cv::bounded<int>::type val_t; typedef cv::bounded<int>::type::constraint_type constraint_type;
int main() { int min = -200; int max = 200; val_t init (10, constraint_type (min, max)); ublas::vector<val_t> v (10, init); ublas::vector<val_t> w = -v; }
This code will throw an exception. The last line invokes: ublas/functional.hpp:70 (scalar_negate), which for some reason calls
constrained.hpp:224, which is: constrained(const value_type & v)
That is, initialize only the value, not the constraint! Any ideas?
It looks like the arithmetic operators may need to be overloaded rather than relying on implicit conversions? The error should occur in a simpler case: val_t x(10, constraint_type(min, max)); val_t neg_x(-x); In Christ, Steven Watanabe

Steven Watanabe wrote:
AMDG
Neal Becker wrote:
This code has an error:
#include <boost/numeric/ublas/vector.hpp> #include <boost/constrained_value.hpp>
namespace cv = boost::constrained_value; namespace ublas = boost::numeric::ublas;
typedef cv::bounded<int>::type val_t; typedef cv::bounded<int>::type::constraint_type constraint_type;
int main() { int min = -200; int max = 200; val_t init (10, constraint_type (min, max)); ublas::vector<val_t> v (10, init); ublas::vector<val_t> w = -v; }
This code will throw an exception. The last line invokes: ublas/functional.hpp:70 (scalar_negate), which for some reason calls
constrained.hpp:224, which is: constrained(const value_type & v)
That is, initialize only the value, not the constraint! Any ideas?
It looks like the arithmetic operators may need to be overloaded rather than relying on implicit conversions?
The error should occur in a simpler case:
val_t x(10, constraint_type(min, max)); val_t neg_x(-x);
Yes, this gives the same error: typedef cv::bounded<int>::type val_t; typedef cv::bounded<int>::type::constraint_type constraint_type; // template<typename V, typename C, typename E> // cv::constrained<V,C,E> & operator- (cv::constrained<V,C,E> c) { // V tmp (c.value()); // c = -tmp; // return c; // } int main() { int min = -200; int max = 200; val_t init (10, constraint_type (min, max)); val_t neg_x(-init); If the above operator overload is uncommented, then it does not throw on the last line above, but it STILL throws on the original vector operation: ublas::vector<val_t> v (10, init); ublas::vector<val_t> w = -v; I can't figure out why the ublas scalar_negate doesn't use my operator-.

On Dec 10, 2008, at 2:57 PM, Neal Becker wrote:
// template<typename V, typename C, typename E> // cv::constrained<V,C,E> & operator- (cv::constrained<V,C,E> c) { // V tmp (c.value()); // c = -tmp; // return c; // }
int main() { int min = -200; int max = 200; val_t init (10, constraint_type (min, max)); val_t neg_x(-init);
If the above operator overload is uncommented, then it does not throw on the last line above, but it STILL throws on the original vector operation:
ublas::vector<val_t> v (10, init); ublas::vector<val_t> w = -v;
I can't figure out why the ublas scalar_negate doesn't use my operator-.
I think that operator would have to be in the constrained_value namespace for ADL to find it when compiling in the ublas namespace; no ADL is necessary when you define the overload in the current namespace. Gordon

From: Neal Becker Steven Watanabe wrote:
It looks like the arithmetic operators may need to be overloaded rather than relying on implicit conversions?
If you mean that they should be overloaded by the library itself, then this is not necessarily a good idea. It is not possible to guess what should be the best return type of arithmetic operators in general case. For some applications returning a constrained value would be OK (but again there is the problem that the constraint may reject the modified value) while for others it's better to return the underlying type.
participants (4)
-
Gordon Woodhull
-
Neal Becker
-
Robert Kawulak
-
Steven Watanabe