help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

conversion of octave_int<short int> to int?


From: John W. Eaton
Subject: conversion of octave_int<short int> to int?
Date: Mon, 12 Nov 2007 16:11:50 -0500

On 12-Nov-2007, Eric Chassande-Mottin wrote:

| hi,
| 
| 
----------------------------------------------------------------------------------------------------
| int16NDArray arcs=myArcs(startNode).int16_array_value();
| 
| [...]
| 
| int arcIndex = (int) arcs(k) - 1;
| [...]
| 
----------------------------------------------------------------------------------------------------
| 
| with the above code, I get the following error at compilation:
| 
| csp.cc:83: note: candidate 2: operator-(float, int) <built-in>
| csp.cc:83: error: conversion from 'octave_int<short int>' to 'int' is 
ambiguous
| /usr/include/octave-2.9.14/octave/oct-inttypes.h:260: note: candidates
| are: octave_int<T>::operator float() const [with T = short int]
| /usr/include/octave-2.9.14/octave/oct-inttypes.h:258: note:
|      octave_int<T>::operator double() const [with T = short int]
| /usr/include/octave-2.9.14/octave/oct-inttypes.h:256: note:
|      octave_int<T>::operator char() const [with T = short int]
| /usr/include/octave-2.9.14/octave/oct-inttypes.h:254: note:
|      octave_int<T>::operator bool() const [with T = short int]
| 
| 
| while  with the change
| 
| double arcIndex = (double) arcs(k) - 1;
| 
| the code does compile. Why is the conversion to int disabled?

It's not disabled, it's just that it is not provided.

Currently, the octave_int class defines the following type
conversions:

  operator bool (void) const { return static_cast<bool> (value ()); }

  operator char (void) const { return static_cast<char> (value ()); }

  operator double (void) const { return static_cast<double> (value ()); }

  operator float (void) const { return static_cast<float> (value ()); }

It may be that these type conversion operators are the only ones
defined because they are the only ones we needed for the Octave
internals.

However, I'm not sure that it is a good idea to have even these, so
they may be removed in the future and replaced with functions instead,
for example:

  double double_value (void) const { return static_cast<double> (value ()); }

though it may cause some trouble if we define these functions for all
types (for example, should octave_int<int64_t> have a conversion to
short?).

In any case, I think having functions for this is better than using
conversion operatores because the functions can't be invoked
automatically, so you are forced to think about the type conversions
that you are making.

If you'd like to discuss how this should be implemented, please start
a discussion on the maintainers list.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]