Question on function overloading

77 views
Skip to first unread message

Bill Gillespie

unread,
Nov 17, 2014, 2:24:06 PM11/17/14
to stan-...@googlegroups.com
I was attempting to take advantage of overloading of user-defined functions. In particular I tried to define functions with the following signatures:

  matrix twoCptModel(real[] time, real[] amt, real[] rate, int[] cmt, int[] evid, 
    real[] CL, real[] Q, real[] V1, real[] V2, real[] ka, int ntMax)

  matrix twoCptModel(real[] time, real[] amt, real[] rate, int[] cmt, int[] evid, 
    real CL, real Q, real V1, real V2, real ka, int ntMax)

That resulted in a compile time error indicating a non-allowable function redefinition. Apparently functions in which the arguments differ only by real vs real[] cannot be overloaded. I confirmed this with the toy functions:

  real test1(real x){
    return x;
  }

  real test1(real[] x){
    return x[1];
  }

They resulted in the error message:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! file4623cde85c6.cpp:304:8: error: redefinition of 'test1_functor__'
struct test1_functor__ {
       ^
file4623cde85c6.cpp:283:8: note: previous definition is here
struct test1_functor__ {
       ^

This is not too hard to work around, but the ability to overload functions that differ in this way would be nice.

Cheers,
Bill

Krzysztof Sakrejda

unread,
Nov 17, 2014, 3:01:42 PM11/17/14
to stan-...@googlegroups.com
I recently ran into (I think) this---see Bob's answer: https://groups.google.com/d/msg/stan-users/tdHs_ZObLoc/6ROfE36YUZUJ

Daniel Lee

unread,
Nov 17, 2014, 3:05:00 PM11/17/14
to stan-...@googlegroups.com
Hi Bill and Krzysztof,

The documentation is incorrect for v2.5.0. This is a known bug, Stan #1096: https://github.com/stan-dev/stan/issues/1096

And it will be fixed for the next release (TBD).




Daniel


--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Nov 19, 2014, 6:02:22 PM11/19/14
to stan-...@googlegroups.com
I'm afraid it will probably be fixed by updating the doc to say that
overloading's not allowed.

The problem is that I introduced functor classes for each function, but
you can't have two classes with the same name. I haven't figured out a good
way to fix the problem that doesn't require a heroic amount of ad hoc
code in the C++ code generator.

- Bob

Ben Goodrich

unread,
Nov 19, 2014, 6:13:24 PM11/19/14
to stan-...@googlegroups.com
On Wednesday, November 19, 2014 6:02:22 PM UTC-5, Bob Carpenter wrote:
The problem is that I introduced functor classes for each function, but
you can't have two classes with the same name.  I haven't figured out a good
way to fix the problem that doesn't require a heroic amount of ad hoc
code in the C++ code generator.

How much heroism would be required to paste the input types onto end of the class name but still refer to it by the basename in the Stan language? Like

foo_real_vector_matrix_realarray

as the class name in C++ but the user can just call

foo(mu, x, Sigma, arr);

Ben

Bob Carpenter

unread,
Nov 19, 2014, 6:24:21 PM11/19/14
to stan-...@googlegroups.com
It's not real vs. var which is the problem, but one arg vs.
two args, or real vs. array.

It's an interesting idea. I'll have to think about how much work it'd
entail. I was thinking was that I'd have to added overloaded operator()
to the functors, but then I'd run into problems with order
of definition.

We need to go in and improve the name mangling in any case
and maybe I can take on both of these issues at once.

I'm totally swamped with to-do items, so we really need to
talk about prioritization or try to bring some more people up
to speed in how to deal with the parser.

- Bob
Reply all
Reply to author
Forward
0 new messages