c++ - gcc deep/double/nested casting -
c++ - gcc deep/double/nested casting -
first code
#include <stdio.h> typedef wchar_t* bstr; wchar_t hello[] = l"hello"; class _bstr_t { public: operator const wchar_t*() const throw() { homecoming hello; } operator wchar_t*() const throw() { homecoming hello; } }; class container { public: operator _bstr_t() { homecoming _bstr_t(); } }; int main() { // gives error (with gcc 4.5.2 @ least): // test.cpp:20:27: error: cannot convert "container" "wchar_t*" in initialization wchar_t *str = container(); printf("%s\n", str); homecoming 0; }
the problem here container()
can casted _bstr_t
, wchar_t*
, but, gcc not.
the problem can solved using manual cast:
wchar_t *str = (_bstr_t)container();
but need avoid manual cast, gcc figure out automatically.
why need because returned container type objects used in calls like
void func(wchar_t* str); func(myobject->container);
where don't want manual casting.
i verified visual studio , not seem back upwards such scenario, too. bad, glad if can provide workaround, if specific case.
update: suggests operator wchar_t* on container, that problem in first place. either leak or crash when destroyed before func() has chance take pointer.
when doing implicit conversions, there's @ one user-defined conversion can happen. msvc behaviour not standard compliant in matter.
c++11 (12.3 conversions):
at 1 user-deļ¬ned conversion (constructor or conversion function) implicitly applied single value.
for implicit conversion work, container
has convert straight wchar_t*
.
c++ visual-c++ gcc
Comments
Post a Comment