Search code examples
c++linuxgccace

warning: deprecated conversion from string constant to ‘ACE_TCHAR*’


I'm attempting to write unit tests in which we call the constructor to a generic class in the form:

void testConstructor() {
    int argc = 2;
    ACE_TCHAR* argv[] = {"Input1", "Input2"};
    MyClass *myClass = new MyClass(argc, argv);
    /**processing**/
}

Think of ACE_TCHAR* the same as char*. The code above results in

warning: deprecated conversion from string constant to ‘ACE_TCHAR*’

I have also tried:

void testConstructor() {
    int argc = 2;
    ACE_TCHAR* argv[2];
    argv[0] = "Input1";
    argv[1] = "Input2";
    MyClass *myClass = new MyClass(argc, argv);
    /**processing**/
}

Which results in the same error.

I read online somewhere that this could be mitigated by using

const ACE_TCHAR* argv[] = {"Input1", "Input2"};

But then the compile fails because of the function signature.

Edit: I'm not allowed to modify the 3rd party code, I'm only writing unit tests for it.

Any ideas?


Solution

  • A string constant is, as the name says, a constant. So to make the warning go away, the proper solution would indeed be to turn the pointer into a const pointer.

    If that cannot be done, you can explicit typecasts

    ACE_TCHAR* argv[] = {const_cast<ACE_TCHAR*>("Input1"),
                         const_cast<ACE_TCHAR*>("Input2")};
    

    or assign the strings to non-constant char arrays first

    char input1[] = "Input1";
    char input2[] = "Input2";
    ACE_TCHAR* argv[] = {input1, input2};