0

I Qt app which works on android. I am writing code which is pretty much C++ 14 standard. Using functions like make_unique & make_shared as well.

However when I try to use std::stoul, it only fails to compile on android. It compiles & works for clang on osx & iOS. Works for Microsoft compiler on windows. But only fails to compile on Android with the following error saying :-

error: 'stoul' is not a member of 'std'

My Qt app uses latest NDK r13b to compile the code for android.

I searched around quite a bit & found similar problems people are facing like here & here. But, I need a way to resolve this on a Qt app. for android

I added the following to my Qt App's .pro file. But doesnt work.

How can I make std::stoul compile in a Qt app for android using latest NDK r13b ?

Also tried adding the following block to my app's .pro file. But doesn't help.

android {
 QMAKE_CXXFLAGS_RELEASE += -std=c++1y
 QMAKE_CXXFLAGS_DEBUG += -std=c++1y
 CONFIG += c++14

 QMAKE_APP_FLAG +=c++_static
}
Community
  • 1
  • 1
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • As long as you are using Qt I would suggest you to use QString as much as you can. You can for example do QString(YOUR_STRING).toLong(). Here YOUR_STRING may as well be a standard C++ string. – Elvis Teixeira Jan 04 '17 at 16:16
  • I am writing this code in core C++ class. this code has to be strictly C++. I don't have that `QString` option – TheWaterProgrammer Jan 04 '17 at 16:18
  • Ok, what compiler are you using in Android? As a last resource you can try the old good sscanf function like sscanf(str.c_str(), "%ld, &yourInteger) if stoll is not available. – Elvis Teixeira Jan 04 '17 at 16:20
  • the compiler is gcc with the version that latest ndk provides – TheWaterProgrammer Jan 04 '17 at 16:36

1 Answers1

1

The easiest way to work around this is to inject the functions into namespace std. Please note that you may need to implement these in a more correct manner, as I've only just scabbed in some code to make this look correct. So make a new file called whatever you like and add this:

#ifndef MYFILE_H
#define MYFILE_H

#if defined(__ANDROID__)
namespace std {
    unsigned long stoul(const std::string &str)
    {
        if (str.length() == 0) {
            return 0;
        }
        auto returnValue = atoi(str.c_str());
        if (returnValue == 0) {
            throw std::invalid_argument("stoul: invalid string " + str);
        }
        return returnValue;
    }
}
#endif

#endif //MYFILE_H
Tyler Lewis
  • 881
  • 6
  • 19