Delphi 单位中的无穷大被转换为 .hpp 文件中的 +INF

发布于 2025-01-16 12:16:26 字数 2364 浏览 0 评论 0 原文

我使用 C++Builder XE6 并编写了以下 Delphi 单元:

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.

编译时,生成以下 .hpp 文件:

// 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

请注意 .pasInfinity 的默认值code> 文件被转换为 .hpp 文件中的 +INF

当我在 C++ 单元中包含 .hpp 文件时,出现以下编译器错误:

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

可以理解,因为 INF 未在 System.Math.hpp< 中定义/code>,但Infinity 是。

如何让编译器将 Infinity(或 HUGE_VAL)输出到 .hpp 文件而不是 +INF

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-23 12:16:26

如何让编译器将 Infinity(或 HUGE_VAL)输出到 .hpp 文件而不是 +INF< /代码>?


AFAIK,你不知道。这就是 Delphi 编译器选择将 Infinity 翻译为 C++ 的方式。请随时向 Embarcadero 提交错误报告

同时,作为一种解决方法,您可以尝试使用 GetJSONDouble “nofollow noreferrer”>{$NODEFINE}{$EXTERNALSYM} 以避免 Delphi 编译器在 .hpp 中输出默认声明,然后使用 {$HPPEMIT} 声明 GetJSONDouble () 自己,例如:

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文