android / ndk

The Android Native Development Kit
1.96k stars 255 forks source link

Possible compiler bug in NDK C++, constructor problem on class #1937

Closed pblagay closed 12 months ago

pblagay commented 12 months ago

Description

Hi, I have a custom string class I use for cross platform development. The android compiler is the only one that doesn't work. Visual Studio and lots of different GCC compilers for the other platforms are fine with it.

Here is a small narrowed down example of the problem. The assignment of the const char to the new string won't compile on android.

class TestString
{
public:
    TestString();
        TestString(TestString& string) {}
        TestString(const char* string);
    const char* m_string;
};

TestString::TestString(const char* string)
{

}

TestString test = "test";

here is the compiler tooltip:

No viable constructor copying variable of type 'LLString' candidate constructor not viable: expects an lvalue for 1st argument candidate constructor not viable: requires 0 arguments, but 1 was provided

And here is the build output:

S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/LustDemoApp/DemoApp.cpp:391:11: error: no viable constructor copying variable of type 'LLString'
        LLString str = "est";
                 ^     ~~~~~
S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/Lust/Engine/String/LLString.h:20:2: note: candidate constructor not viable: expects an lvalue for 1st argument
        LLString(LLString& string)
        ^
S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/Lust/Engine/String/LLString.h:37:2: note: candidate constructor not viable: no known conversion from 'LLString' to 'const char *' for 1st argument
        LLString(const char* string);                                                   // assign and allocate char string
        ^
S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/Lust/Engine/String/LLString.h:38:2: note: candidate constructor not viable: no known conversion from 'LLString' to 'const u16 *' (aka 'const unsigned short *') for 1st argument
        LLString(const u16* string);                                                            // assign and allocate u16 string
        ^
S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/Lust/Engine/String/LLString.h:39:2: note: candidate constructor not viable: no known conversion from 'LLString' to 'u16' (aka 'unsigned short') for 1st argument
        LLString(u16 size, bool isWide = false);                                        // assign empty space of size
        ^
S:/Coding/AtariST/Projects/Android/WallCalendarGameActivity/app/src/main/cpp/Lust/Engine/String/LLString.h:19:2: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
        LLString();                                                                                             // default (string == NULL)
        ^

I found that if I remove the reference constructor it won't fail. Obviously that isn't ideal and I don't think it should be working this way. Can you tell me if this is expected and if so why? Or if it's a genuine compiler bug?

Affected versions

r25

Canary version

No response

Host OS

Windows

Host OS version

Windows 11

Affected ABIs

armeabi-v7a, arm64-v8a, x86, x86_64

Build system

CMake

Other build system

No response

minSdkVersion

30

Device API level

32

DanAlbert commented 12 months ago

It works fine with the newer compiler. I tested with r27 because it's what I have. r26 will be out tomorrow and might also be fixed.