const char*特に文字列リテラルを使用する場合は、 を使用します。
char* bad = "Foo"; // wrong!
bad[0] = 'f'; // undefined behavior!
正しい:
const char* s = "Foo"; // correct
obj(s); // automatically converted to python string
または、次を使用できます。
std::string s = "Bar"; // ok: std::string
obj(s); // automatically converted to python string
obj("Baz"); // ok: it's actually a const char*
char c = 'X'; // ok, single character
obj(c); // automatically converted to python string
signed char d = 42; // careful!
obj(d); // converted to integer (same for unsigned char)
boost::pythonconst char*、std::stringおよびcharPython3の文字列コンバーターを定義しますstd::wstring。適切なコンバーターを選択するために、boost は特殊なテンプレート (組み込み型用に定義されている) を介して型を一致させようとします。char*が一致しないためconst char*、 のコンバーターchar*が登録されていないため、変換に失敗します。
適切な がある場合は、python に渡す前char*に a にキャストします。const char*
char* p = new char[4];
memcpy(p,"Foo",4); // include terminating '\0'
obj( const_cast<const char*>(p) );
delete [] p;