Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing an iteration object by reference to a function #967

Closed
ghost opened this issue Feb 11, 2018 · 7 comments
Closed

Passing an iteration object by reference to a function #967

ghost opened this issue Feb 11, 2018 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ghost
Copy link

ghost commented Feb 11, 2018

Bug Report

  • What is the issue you have?
    I cannot pass a JSON iteration object by reference to a function

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

nlohmann::json j;
std::ifstream if( "C:\\example.json" );
j << if;
if.close( );

/*...*/

void square( int *in )
{
    in *= in;
}

/*...*/

square( &j[ "int" ] );
  • What is the expected behavior?
    To be able to pass the object by reference, rather than this:
int buf = j[ "int" ];
square( &buf );
j[ "int" ] = buf;
  • And what is the actual behavior instead?
    Error (active) E0167 argument of type "nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer> *" is incompatible with parameter of type "int *"

  • Which compiler and operating system are you using? Is it a supported compiler?
    Visual Studio 2017 15.5.3, Windows 10

  • Did you use a released version of the library or the version from the develop branch?
    Released

  • If you experience a compilation error: can you compile and run the unit tests?
    Yes

@c-m-w
Copy link

c-m-w commented Feb 11, 2018

+1

@pboettch
Copy link
Contributor

One problem is that you're mixing references and pointers. Your square-function expects a reference to in int (int &) but you're passing a pointer (&j["int"]). (The same is true for your "rather than this"-example, which won't work either).

@nlohmann
Copy link
Owner

You can use the get_ref function to get a reference and the get_ptr function to get a point to a stored value.

However, the library does not store integers as int, but uses uint64_t for unsigned values and int64_t for signed values. You can check the value with is_number_unsigned() and is_number_integer(), respectively. In either case, get_ref<int&>() or get_ptr<int*>() will not work.

You could template your square function to work with any number reference type, and then pass a reference to uint64_t or int64_t. It could be even easier to pass json & and directly manipulate the value.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Feb 11, 2018
@nlohmann
Copy link
Owner

@J-e-r-e-m-i-a-h Do you need further assistance?

@ghost
Copy link
Author

ghost commented Feb 13, 2018

The example is wrong, by the way; the square function is improper (I made the post late at night). I just copied the parameters for a Windows function I'm using and then simplified it in order to post an example. Either way, I do require passing a variable by reference like &i for int i as a parameter to the function.

As of now, I'm trying to figure it out, I'll let you know if I can't figure out how to use get_ref :P I'm pretty sure your proposed solution is correct. Here's what I've got so far, let me know if I'm on the right track :)

if ( j[ "int" ].is_number_unsigned( ) )
    square( j[ "int" ].get_ref<uint64_t&>( ) );
else if ( j[ "int" ].is_number_integer( ) )
    square( j[ "int" ].get_ref<int64_t&>( ) );

@nlohmann
Copy link
Owner

Looks right.

@ghost
Copy link
Author

ghost commented Feb 13, 2018

All is good then! Thanks for the help! I love this resource, absolutely amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants