0

I'm using C++Builder XE6 and wrote the following Delphi unit:

unit JSONUtils;

interface

uses
  System.JSON, System.Math;

function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double = Infinity): Double;

implementation

function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double): Double;
begin
  Result := Value.GetValue<Double>(Path, Default);
end;

end.

When compiled, the following .hpp file is generated:

// CodeGear C++Builder
// Copyright (c) 1995, 2014 by Embarcadero Technologies, Inc.
// All rights reserved

// (DO NOT EDIT: machine generated header) 'JSONUtils.pas' rev: 27.00 (Windows)

#ifndef JsonutilsHPP
#define JsonutilsHPP

#pragma delphiheader begin
#pragma option push
#pragma option -w-      // All warnings off
#pragma option -Vx      // Zero-length empty class member 
#pragma pack(push,8)
#include <System.hpp>   // Pascal unit
#include <SysInit.hpp>  // Pascal unit
#include <System.JSON.hpp>  // Pascal unit
#include <System.Math.hpp>  // Pascal unit

//-- user supplied -----------------------------------------------------------

namespace Jsonutils
{
//-- type declarations -------------------------------------------------------
//-- var, const, procedure ---------------------------------------------------
extern DELPHI_PACKAGE double __fastcall GetJSONDouble(System::Json::TJSONValue* Value, System::UnicodeString Path, double Default = +INF);
}   /* namespace Jsonutils */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_JSONUTILS)
using namespace Jsonutils;
#endif
#pragma pack(pop)
#pragma option pop

#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif  // JsonutilsHPP

Note that the default value of Infinity in the .pas file is translated to +INF in the .hpp file.

When I include the .hpp file in a C++ unit, I get the following compiler error:

[bcc32 Error] JSONUtils.hpp(26): E2451 Undefined symbol 'INF'

Understandable, because INF is not defined in System.Math.hpp, but Infinity is.

How do I get the compiler to output Infinity (or HUGE_VAL) to the .hpp file instead of +INF?

2
  • @David If I write double X = Infinity, Y = HUGE_VAL;, the comparison (X == Y) evaluates to true. Both X and Y contain the value 0x7FF0000000000000. Mar 24 at 9:49
  • Oh, I guess I'm wrong then, sorry Mar 24 at 10:31

1 Answer 1

1

How do I get the compiler to output Infinity (or HUGE_VAL) to the .hpp file instead of +INF?

AFAIK, you don't. That is simply how the Delphi compiler chose to translate Infinity for C++. Feel free to submit a bug report to Embarcadero about it.

In the meantime, as a workaround, what you could try instead is declaring GetJSONDouble with {$NODEFINE} or {$EXTERNALSYM} to avoid the Delphi compiler from outputting a default declaration in the .hpp, and then use {$HPPEMIT} to declare GetJSONDouble() yourself, eg:

unit JSONUtils;

interface

uses
  System.JSON, System.Math;

{$EXTERNALSYM GetJSONDouble}
function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double = Infinity): Double;

{$HPPEMIT OPENNAMESPACE}
{$HPPEMIT 'extern DELPHI_PACKAGE double __fastcall GetJSONDouble(System::Json::TJSONValue* Value, System::UnicodeString Path, double Default = System::Math::Infinity);'}
{$HPPEMIT CLOSENAMESPACE}

implementation

function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double): Double;
begin
  Result := Value.GetValue<Double>(Path, Default);
end;

end.
1
  • That did the trick! I knew about {$HPPEMIT '...'}, but somehow missed {$NODEFINE} and {$EXTERNALSYM ...}. Thank you very much, Remy. I really appreciate your continuing effort. Mar 25 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.