TwCopyStdStringToLibrary (function)

void TwCopyStdStringToLibrary(std::string& dest, const std::string& src)


This function is related to variables of type TW_TYPE_STDSTRING (ie. of type std::string defined by the standard template library (STL)). It is the counterpart of the function provided to TwCopyStdStringToClientFunc.

It copies the string src handled by the client application (your program) to the string dest handled by the AntTweakBar library.

It is useful if you are adding variables of type TW_TYPE_STDSTRING to a tweak bar using TwAddVarCB in order to manage the variable through callback functions. See example below.

This function is required because memory allocated by the application (your program) cannot be resized or deleted by a dynamic library (like AntTweakBar) and vice versa. Thus it should be called for copying a string in order to avoid bad memory handling between the two modules.



Reference to the destination string handled by the AntTweakBar library.


The source string handled by your application.


The following example adds a std::string to a tweak bar using TwAddVarCB which requires the use of TwCopyStdStringToLibrary.

std::string s = "a STL string";
// ...
void TW_CALL SetMyStdStringCB(const void *value, void * /*clientData*/)
  // Set: copy the value of s from AntTweakBar
  const std::string *srcPtr = static_cast<const std::string *>(value);
  s = *srcPtr;
void TW_CALL GetMyStdStringCB(void *value, void * /*clientData*/)
  // Get: copy the value of s to AntTweakBar
  std::string *destPtr = static_cast<std::string *>(value);
  TwCopyStdStringToLibrary(*destPtr, s); // the use of TwCopyStdStringToLibrary is required here
// ...
TwAddVarCB(bar, "s", TW_TYPE_STDSTRING, SetMyStdStringCB, GetMyStdStringCB, NULL, "");

See TwCopyStdStringToClientFunc for a simpler example that uses TwAddVarRW.

See also