Friday, July 5, 2013

Why doesn't implicit conversion work with wide ostream?

Newsgroup: comp.lang.c++

Subject: Why doesn't implicit conversion work with wide ostream?

From: Martin Ba <0xcdcdcdcd@...>

Date: Fri, 05 Jul 2013 16:01:06 +0200





Question @ SO:

http://stackoverflow.com/questions/17486156/why-doesnt-implicit-conversion-work-with-wide-ostream



I have some behaviour that I do not understand. I observed this on

VS2005, but [IDEONE (using GCC 4.7.2) outputs][http://ideone.com/doSHvq]

basically the same.



Here's the code:



#include <iostream>

#include <string>



struct UserString {

const char* p;

operator const char*() const {

std::cout << "! " << __FUNCTION__ << std::endl;

return p;

}



UserString()

: p ("UserString")

{ }

};



struct WUserString {

const wchar_t* p;

operator const wchar_t*() const {

std::cout << "! " << __FUNCTION__ << std::endl;

return p;

}



WUserString()

: p (L"WUserString")

{ }

};





int main() {

using namespace std;

cout << "String Literal" << endl;

cout << string("std::string") << endl;

cout << UserString() << endl;

cout << static_cast<const char*>(UserString()) << endl;



wcout << L"WString Literal" << endl;

wcout << wstring(L"std::wstring") << endl;

wcout << WUserString() << endl;

wcout << static_cast<const wchar_t*>(WUserString()) << endl;



return 0;

}



Here's the output:



String Literal

std::string

! operator const char* **** "works"

UserString ****

! operator const char*

UserString

WString Literal

std::wstring

! operator const wchar_t* **** "doesn't" - op<<(void*) is used

0x80491b0 ****

! operator const wchar_t*

WUserString





What's going on here?!?





cheers,

Martin





--

Like any language, C++ allows you to shoot yourself

in the foot -- but with C++, you sometimes don't

realize you shot yourself until it's too late. (Jeff Langr)







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?36223-Why-doesn-t-implicit-conversion-work-with-wide-ostream&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment