help-octave
[Top][All Lists]
Advanced

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

Re: Changing data type in octave_value_list [actually input args from DE


From: John W. Eaton
Subject: Re: Changing data type in octave_value_list [actually input args from DEFUN_DLD]
Date: Mon, 21 Aug 2006 15:57:44 -0400

On 11-Aug-2006, Joshua Rigler wrote:

| I think what you suggested *should* work, but it's just a slightly 
| different version of something I already tried, which did not work.  So, 
| I tried to come up with something even simpler in order to figure out 
| what was going on, and discovered some very repeatable strangeness in 
| Octave's dynamically loaded functions.  Would you, or other interested 
| parties, look at the following short code snippet, which should compile 
| easily using 'mkoctfile blah.cc', and consider a couple of questions?
| 
| 
|    #include <octave/oct.h>
| 
|    DEFUN_DLD(blah, args, nargout, "...")
|    {
| 
|      int i;
|      octave_value_list rets;
| 
|      rets = args;
| 
|      for (i=0; i<args.length(); i++) {
|        if (args(i).is_string()) {
|          args(i) = octave_value (9999);
|          rets(i) = octave_value (9999);
|        }
|      }
|      //return rets;
|      return args;
|    }
| 
| 
| I test this by [un]commenting the 'return rets;' line, recompiling, and 
| re-running blah.oct with various inputs that are a mixture of strings 
| and non-strings.  What I expect is that strings in both args(i) and 
| rets(i) will get changed into the scalar 9999.  What actually happens is 
| that *only* rets(i) gets modified, while args(i) remains unchanged.
| 
| What is so special about args here?
| Why does it seem to be impossible to change any of its elements?
| Is it not an octave_value_list like rets?

Yes, they are both octave_value_list objects, but args it is const, so
you are not supposed to be able to change it.

The reason it is const is to preserve the semantics of the scripting
language, which doesn't allow fucntion arguments to be modified.

It's unfortuante that there is no error message or at least a warning
about this when you compile your code.

What happens is that

  args(i)

for is valid and since args is const it returns a temporary object
instead of a reference to the i-th value of args.  So then when we
have

  args(i) = octave_value (9999);

you are just assigning the RHS to a temporary, so the operation has no
effect.  Hmm.  Is that right?  Maybe some C++ guru will see a way to
fix the octave_value_list (or Array template) declarations so that
this operation will be flagged.  I don't see how at the moment.

jwe


reply via email to

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