Menu

#15 Passes literal instead of value to ctor expecting string

open
parser (2)
5
2003-12-18
2003-12-18
Dan Ingold
No

Given a class whose ctor takes a string, xParam does
not appear to correctly init the class with a literal (i.e.,
constant) parameter. xParam passes the name of the
constant to the ctor, rather than its value. For
example, the attached test program prints

Test contains "INIT"

rather than

Test contains "xyzzy"

I assume this occurs because under the relaxed syntax
rules, xParam interprets INIT as a string instead of a
constant. Shouldn't xParam look for the token in the
symbol table before presuming it is a string?

If I am missing some fundamental concept here, please
let me know. Thank you.

Regards,
Dan

Discussion

  • Dan Ingold

    Dan Ingold - 2003-12-18

    Example code demonstrating bug

     
  • Ronnie Maor

    Ronnie Maor - 2003-12-22

    Logged In: YES
    user_id=335649

    After checking the problem on my computer, I found two
    relevant issues:

    1) In the statement param_const("INIT",Test::INIT), the type
    of Test::INIT is "const char *", so XParam tries to register
    the constant, but can't finish the registeration since it waits
    for the type "const char *" to be registered first (which never
    happens, and shouldn't).
    You can see this if you try to get help from XParam on the
    topic "PENDING". One way to do this is give the following in
    your argv: {"dummy", "! PENDING"}.
    The solution is to copy the literal into a string, and give that
    string as the parameter to param_const, or just convert to
    string inline.

    2) If you do the above, you'll still need to add an xparam_init
    call before input() (best practice is having it as the first line
    of main()). Otherwise XParam parses the string first, and only
    performs the initialization (which executes the registration)
    when it tries to build the value. At that point it has already
    decided that INIT is a string and not a constant. The way to
    see this is to call input() twice - you'll see that the second
    time works, since the registration is triggered during the first
    call.

    This second issue is indeed a bug, so thanks. I hope we've
    given you enough information to be able to circumvent it.

    Ronnie

     

Log in to post a comment.