Search code examples
c++clangc++14travis-ciclang++

error: cannot pass object of non-trivial type 'std::string' and more errors


I'm running a build in Travis CI, and during the compilation, this error occurs. I tried specifying the C++ compiler and tried using g++ but that resulted in more errors.

$ clang -I ./jsoncpp/include/ -L ./jsoncpp/build/debug/lib -std=c++14 -v test.cpp -o buildtest.exe
...
./IBMWatson.h:79:44: error: cannot pass object of non-trivial type 'std::string'
      (aka 'basic_string<char>') through variadic function; call will abort at
      runtime [-Wnon-pod-varargs]
                curl_easy_setopt(curl, CURLOPT_PASSWORD, apikey); /* Par...
                                                         ^
./IBMWatson.h:81:39: error: cannot pass object of non-trivial type 'std::string'
      (aka 'basic_string<char>') through variadic function; call will abort at
      runtime [-Wnon-pod-varargs]
                curl_easy_setopt(curl, CURLOPT_URL, url); /* Sets Regio...
                                                    ^
./IBMWatson.h:104:102: warning: result of comparison against a string literal is
      unspecified (use strncmp instead) [-Wstring-compare]
  ...+ response.length(), &root, &err) || funcName == "returnVoices" || funcN...
...
4 warnings and 4 errors generated.
The command "clang -I ./jsoncpp/include/ -L ./jsoncpp/build/debug/lib -std=c++14 -v test.cpp -o buildtest.exe" exited with 1.

Solution

  • curl_easy_setopt is a C function, where variadic actually means the <cstdarg>'s ... parameter. It accepts only trivial types, which std::string is not (i.e., it cannot be copied with memcpy, and instead, a non-trivial copy-constructor is involved); otherwise the behavior is undefined or only conditionally supported. In order to pass a string value, use its const char* representation obtained with c_str():

    curl_easy_setopt(curl, CURLOPT_PASSWORD, apikey.c_str());
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());