[C++-sig] Re: implicitly_convertible and char*

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Jun 29 22:56:54 CEST 2004


I don't really feel competent in this matter, but I believe char const* and
const char* are two different things. Did you try it with "const char*"?

--- Dusty Leary <dleary at gmail.com> wrote:
> > 
> > Oh!
> > 
> > char* is not a C++ string, and there's no built-in converter for that.
> > I note that you're casting away the const-ness of v.c_str() above;
> > that's very bad juju.  If you make char* into char const* everywhere,
> > your example should work.  Alternatively, you can also register a
> > custom from_python converter for char*... but I recommend against it.
> > 
> 
> yeah, I just wrote up the simple test like that because I was lazy and
> didn't think it would make a difference.  the same problem occurs with
> const char*
> 
> new source code:
> ===================================================
> #define BOOST_PYTHON_STATIC_LIB
> 
> #include <boost/python.hpp>
> using namespace boost::python;
> 
> struct X {
>     std::string v;
>     X(const std::string& s) : v(s) {}
>     operator char const*() const { return v.c_str(); }
> };
> 
> void test_x(X& x) {
>     char const* x_string = x;
>     printf("test_x: %s\n", x_string);
> }
> 
> void test_string(char const* s) {
>     printf("test_string: %s\n", s);
> }
> 
> BOOST_PYTHON_MODULE(test_string) {
>     class_<X>("X", init<const std::string&>());
>     def("test_x", test_x);
>     def("test_string", test_string);
>     implicitly_convertible<X, char const*>();
> }
> ===================================================
> 
> >>> import test_string
> >>> x = test_string.X("foo")
> >>> test_string.test_string(x)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> Boost.Python.ArgumentError: Python argument types in
>     test_string.test_string(X)
> did not match C++ signature:
>     test_string(char const *)
> >>>



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 




More information about the Cplusplus-sig mailing list