HaxeFoundation / hxcpp

Runtime files for c++ backend for haxe
Other
298 stars 191 forks source link

Error: conversion from 'long' to 'const Dynamic' is ambiguous #621

Open thejustinwalsh opened 7 years ago

thejustinwalsh commented 7 years ago

When compiling SiON for macOS (lime build mac -debug) I am getting the following compiler error:

Error: ./src/org/si/sion/sequencer/base/MMLParser.cpp:756:45: error: conversion from 'long' to 'const Dynamic' is ambiguous
int defaultValue = __o_defaultValue.Default(-2147483648);
                                            ^~~~~~~~~~~
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:21:4: note: candidate constructor
   Dynamic(int inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:22:4: note: candidate constructor
   Dynamic(short inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:23:4: note: candidate constructor
   Dynamic(unsigned int inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:24:4: note: candidate constructor
   Dynamic(unsigned short inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:25:4: note: candidate constructor
   Dynamic(unsigned char inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:26:4: note: candidate constructor
   Dynamic(signed char inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:28:4: note: candidate constructor
   Dynamic(bool inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:29:4: note: candidate constructor
   Dynamic(double inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:30:4: note: candidate constructor
   Dynamic(float inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:31:4: note: candidate constructor
   Dynamic(cpp::Int64 inVal);
   ^
/usr/local/lib/haxe/lib/hxcpp/3,4,64/include/Dynamic.h:32:4: note: candidate constructor
   Dynamic(cpp::UInt64 inVal);
   ^
1 error generated.

When compiling for iOS (lime build ios -debug) the compilation results in a few more warnings, and still fails to compile:

warning: integer literal is too large to be represented in type 'long' and is subject to undefined behavior under C++98, interpreting as 'unsigned long'; this literal will have type 'long long' in C++11 onwards [-Wc++11-compat]
HXDLIN(  38)            INT_MIN_VALUE = (int)-2147483648;

Which is generated from the following source: https://github.com/thejustinwalsh/SiON/blob/master/src/org/si/sion/sequencer/base/MMLParser.hx#L476-L478

Using the definition of INT_MIN_VALUE

public static inline var INT_MIN_VALUE = -2147483648;
thejustinwalsh commented 7 years ago

Similar error on windows as well:

./src/org/si/sion/sequencer/base/MMLParser.cpp(756): error C2664: 'Dynamic Dynamic::Default(const Dynamic &)': cannot convert argument 1 from 'unsigned long' to 'const Dynamic &'
./src/org/si/sion/sequencer/base/MMLParser.cpp(756): note: Reason: cannot convert from 'unsigned long' to 'const Dynamic'
./src/org/si/sion/sequencer/base/MMLParser.cpp(756): note: No constructor could take the source type, or constructor overload resolution was ambiguous
thejustinwalsh commented 7 years ago

haxe -version

3.4.2 (git build master @ 890f8c7)

haxelib list

hxcpp: [3.4.64]
lime-samples: [4.0.1]
lime: 2.9.1 5.2.1 [5.3.0]
openfl-samples: 5.1.1 [6.0.0]
openfl: 3.6.1 5.1.5 [6.0.1]
thejustinwalsh commented 7 years ago

It appears that the compiler will assign the type of long for signed integer literals although I am not sure under what circumstances the literal is defined as a long.

I was able to resolve the issue by adding the proper constructors to handle long and unsigned long

// Dynamic.h
Dynamic(long inVal);
Dynamic(unsigned long inVal);

//Dynamic.cpp
Dynamic::Dynamic(long inVal)
{
   mPtr = fromInt(inVal);
}

Dynamic::Dynamic(unsigned long inVal)
{
   mPtr = fromInt(inVal);
}
aW4KeNiNG commented 5 years ago

I have this problem in Mac too:

error: conversion from 'size_t' (aka 'unsigned long') to '::Dynamic' is ambiguous

with the next code:

var num:cpp.SizeT = 10;
trace('My num is ${num}');

The PR was changed in https://github.com/HaxeFoundation/hxcpp/commit/d9c02ec114f7db00070b46a91a45ff3bfc353530