How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Boying Xu
According to the online document
https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html

By default, wxString uses std::basic_string specialized for the platform-dependent wchar_t type, meaning that it is not memory-efficient for ASCII strings, especially under Unix platforms where every ASCII character, normally fitting in a byte, is represented by a 4 byte wchar_t.

This is very handy as in my code I can do static_cast<std::wstring>(wxString_type_var> and continue the process using the code applied to wchar_t

Now my question is how I determinate the wxString, string, and wstring at complier time to get proper mapping to overloaded function for string and wstring
I would prefer to map wxString, wstring -> version1 and string to version 2


#include <wx/wx.h>

#include <iostream>
#include <map>
#include <memory>
#include <typeindex>
#include <typeinfo>
#include <type_traits> // C++0x

namespace foo {

struct v1_tag {};
struct v2_tag {};

template <typename T>
struct someclass_version_tag;

template <>
struct someclass_version_tag<std::char_traits<std::wstring>> {
   
typedef std::wstring type;
   
typedef v1_tag       tag;
};

template <>
struct someclass_version_tag<std::char_traits<std::string>> {
   
typedef std::string type;
   
typedef v2_tag      tag;
};

void test(v1_tag, std::wstring & x) {
    std
::cout << "v1_tag" << std::endl;
}

void test(v2_tag, std::string & x) {
    std
::cout << "v2_tag" << std::endl;
}

template <typename T = std::string>
static void create(const T & var) {
    test
(someclass_version_tag<std::char_traits<T>>::tag(), static_cast<someclass_version_tag<std::char_traits<T>>::type>(var));
}
};

int main(int argc, char * argv[]) {
   
//wxString     tmp  = "wxString";
   
//foo::create<wxString>(tmp);
    std
::wstring tmp2 = L"wstring";
    foo
::create<std::wstring>(tmp2);
    std
::string tmp3 = "string";
    foo
::create<std::string>(tmp3);
   
return 0;
}



If I remove the comments in main, the complier will fail, so what is the correct way to mapping the type?

Thanks in advance.

Environment: VC++ 2013

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

oneeyeman
Hi,

On Tue, Feb 21, 2017 at 10:15 PM, Boying Xu <[hidden email]> wrote:

> According to the online document
> https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html
>
>> By default, wxString uses std::basic_string specialized for the
>> platform-dependent wchar_t type, meaning that it is not memory-efficient for
>> ASCII strings, especially under Unix platforms where every ASCII character,
>> normally fitting in a byte, is represented by a 4 byte wchar_t.
>
>
> This is very handy as in my code I can do
> static_cast<std::wstring>(wxString_type_var> and continue the process using
> the code applied to wchar_t
>
> Now my question is how I determinate the wxString, string, and wstring at
> complier time to get proper mapping to overloaded function for string and
> wstring
> I would prefer to map wxString, wstring -> version1 and string to version 2

Can't you just call ToStdWstring()?

Thank you.

>
>
> #include <wx/wx.h>
>
> #include <iostream>
> #include <map>
> #include <memory>
> #include <typeindex>
> #include <typeinfo>
> #include <type_traits> // C++0x
>
> namespace foo {
>
> struct v1_tag {};
> struct v2_tag {};
>
> template <typename T>
> struct someclass_version_tag;
>
> template <>
> struct someclass_version_tag<std::char_traits<std::wstring>> {
>     typedef std::wstring type;
>     typedef v1_tag       tag;
> };
>
> template <>
> struct someclass_version_tag<std::char_traits<std::string>> {
>     typedef std::string type;
>     typedef v2_tag      tag;
> };
>
> void test(v1_tag, std::wstring & x) {
>     std::cout << "v1_tag" << std::endl;
> }
>
> void test(v2_tag, std::string & x) {
>     std::cout << "v2_tag" << std::endl;
> }
>
> template <typename T = std::string>
> static void create(const T & var) {
>     test(someclass_version_tag<std::char_traits<T>>::tag(),
> static_cast<someclass_version_tag<std::char_traits<T>>::type>(var));
> }
> };
>
> int main(int argc, char * argv[]) {
>     //wxString     tmp  = "wxString";
>     //foo::create<wxString>(tmp);
>     std::wstring tmp2 = L"wstring";
>     foo::create<std::wstring>(tmp2);
>     std::string tmp3 = "string";
>     foo::create<std::string>(tmp3);
>     return 0;
> }
>
>
>
> If I remove the comments in main, the complier will fail, so what is the
> correct way to mapping the type?
>
> Thanks in advance.
>
> Environment: VC++ 2013
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to [hidden email]
> or visit http://groups.google.com/group/wx-users

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Boying Xu
Unfortunately, not
First I would prefire to call static_cast<std::wstring> wxstr_var, instead of ToStdWstring(), the latter is pure run-time execution code and not exists in std::basic_strings
The limitation here is the type must be detected during compiler time, so compiler can choose the proper overload function (test())
static_cast still can not be properlly used at complier time.
One ugly workaround is include <wx/wx.h> at the beginning of the header file and make a special tag mapping like this:


template <>
struct someclass_version_tag<std::char_traits<wxString>> {
   
typedef std::wstring type;
   
typedef v1_tag      tag;
};

However, this will limit my module be published with wx framework or I shall split the module for wx and non-wx users
Additionally this will break the VC++ SDL check for some strange reason

So I really looking for a traits version for overloading
mechanism.
The idea is from this post: https://stackoverflow.com/questions/4522375/switch-passed-type-from-template



PS
I also find a way called
SFINAE (Substitution Failure Is Not An Error), however that way is a bit hack, complier version requied,  and going too far, which I don't really want to try it(getting into limbo):)






On Wednesday, February 22, 2017 at 3:13:55 PM UTC+8, Igor Korot wrote:
Hi,

On Tue, Feb 21, 2017 at 10:15 PM, Boying Xu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2Ilct6Z1DAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">xubo...@...> wrote:

> According to the online document
> <a href="https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Ffossies.org%2Fdox%2FwxWidgets-3.1.0%2FclasswxString.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEvOcKgB7fEsM2g3g7SDkkJpwXo6g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Ffossies.org%2Fdox%2FwxWidgets-3.1.0%2FclasswxString.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEvOcKgB7fEsM2g3g7SDkkJpwXo6g&#39;;return true;">https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html
>
>> By default, wxString uses std::basic_string specialized for the
>> platform-dependent wchar_t type, meaning that it is not memory-efficient for
>> ASCII strings, especially under Unix platforms where every ASCII character,
>> normally fitting in a byte, is represented by a 4 byte wchar_t.
>
>
> This is very handy as in my code I can do
> static_cast<std::wstring>(wxString_type_var> and continue the process using
> the code applied to wchar_t
>
> Now my question is how I determinate the wxString, string, and wstring at
> complier time to get proper mapping to overloaded function for string and
> wstring
> I would prefer to map wxString, wstring -> version1 and string to version 2

Can't you just call ToStdWstring()?

Thank you.

>
>
> #include <wx/wx.h>
>
> #include <iostream>
> #include <map>
> #include <memory>
> #include <typeindex>
> #include <typeinfo>
> #include <type_traits> // C++0x
>
> namespace foo {
>
> struct v1_tag {};
> struct v2_tag {};
>
> template <typename T>
> struct someclass_version_tag;
>
> template <>
> struct someclass_version_tag<std::char_traits<std::wstring>> {
>     typedef std::wstring type;
>     typedef v1_tag       tag;
> };
>
> template <>
> struct someclass_version_tag<std::char_traits<std::string>> {
>     typedef std::string type;
>     typedef v2_tag      tag;
> };
>
> void test(v1_tag, std::wstring & x) {
>     std::cout << "v1_tag" << std::endl;
> }
>
> void test(v2_tag, std::string & x) {
>     std::cout << "v2_tag" << std::endl;
> }
>
> template <typename T = std::string>
> static void create(const T & var) {
>     test(someclass_version_tag<std::char_traits<T>>::tag(),
> static_cast<someclass_version_tag<std::char_traits<T>>::type>(var));
> }
> };
>
> int main(int argc, char * argv[]) {
>     //wxString     tmp  = "wxString";
>     //foo::create<wxString>(tmp);
>     std::wstring tmp2 = L"wstring";
>     foo::create<std::wstring>(tmp2);
>     std::string tmp3 = "string";
>     foo::create<std::string>(tmp3);
>     return 0;
> }
>
>
>
> If I remove the comments in main, the complier will fail, so what is the
> correct way to mapping the type?
>
> Thanks in advance.
>
> Environment: VC++ 2013
>
> --
> Please read <a href="http://www.wxwidgets.org/support/mlhowto.htm" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;">http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2Ilct6Z1DAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">wx-users+u...@googlegroups.com
> or visit <a href="http://groups.google.com/group/wx-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;">http://groups.google.com/group/wx-users

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

oneeyeman
Hi,

On Wed, Feb 22, 2017 at 3:08 AM, Boying Xu <[hidden email]> wrote:
> Unfortunately, not
> First I would prefire to call static_cast<std::wstring> wxstr_var, instead
> of ToStdWstring(), the latter is pure run-time execution code and not exists
> in std::basic_strings
> The limitation here is the type must be detected during compiler time, so
> compiler can choose the proper overload function (test())
> static_cast still can not be properlly used at complier time.
> One ugly workaround is include <wx/wx.h> at the beginning of the header file
> and make a special tag mapping like this:

Ok.
Now I wonder why do you need to split between string and wstring?
Why not use std::wstring explicitly?

Thank you.

>
>
> template <>
> struct someclass_version_tag<std::char_traits<wxString>> {
>     typedef std::wstring type;
>     typedef v1_tag      tag;
> };
>
> However, this will limit my module be published with wx framework or I shall
> split the module for wx and non-wx users
> Additionally this will break the VC++ SDL check for some strange reason
>
> So I really looking for a traits version for overloading mechanism.
> The idea is from this post:
> https://stackoverflow.com/questions/4522375/switch-passed-type-from-template
>
>
>
> PS
> I also find a way called SFINAE (Substitution Failure Is Not An Error),
> however that way is a bit hack, complier version requied,  and going too
> far, which I don't really want to try it(getting into limbo):)
>
>
>
>
>
>
> On Wednesday, February 22, 2017 at 3:13:55 PM UTC+8, Igor Korot wrote:
>>
>> Hi,
>>
>> On Tue, Feb 21, 2017 at 10:15 PM, Boying Xu <[hidden email]> wrote:
>> > According to the online document
>> > https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html
>> >
>> >> By default, wxString uses std::basic_string specialized for the
>> >> platform-dependent wchar_t type, meaning that it is not
>> >> memory-efficient for
>> >> ASCII strings, especially under Unix platforms where every ASCII
>> >> character,
>> >> normally fitting in a byte, is represented by a 4 byte wchar_t.
>> >
>> >
>> > This is very handy as in my code I can do
>> > static_cast<std::wstring>(wxString_type_var> and continue the process
>> > using
>> > the code applied to wchar_t
>> >
>> > Now my question is how I determinate the wxString, string, and wstring
>> > at
>> > complier time to get proper mapping to overloaded function for string
>> > and
>> > wstring
>> > I would prefer to map wxString, wstring -> version1 and string to
>> > version 2
>>
>> Can't you just call ToStdWstring()?
>>
>> Thank you.
>>
>> >
>> >
>> > #include <wx/wx.h>
>> >
>> > #include <iostream>
>> > #include <map>
>> > #include <memory>
>> > #include <typeindex>
>> > #include <typeinfo>
>> > #include <type_traits> // C++0x
>> >
>> > namespace foo {
>> >
>> > struct v1_tag {};
>> > struct v2_tag {};
>> >
>> > template <typename T>
>> > struct someclass_version_tag;
>> >
>> > template <>
>> > struct someclass_version_tag<std::char_traits<std::wstring>> {
>> >     typedef std::wstring type;
>> >     typedef v1_tag       tag;
>> > };
>> >
>> > template <>
>> > struct someclass_version_tag<std::char_traits<std::string>> {
>> >     typedef std::string type;
>> >     typedef v2_tag      tag;
>> > };
>> >
>> > void test(v1_tag, std::wstring & x) {
>> >     std::cout << "v1_tag" << std::endl;
>> > }
>> >
>> > void test(v2_tag, std::string & x) {
>> >     std::cout << "v2_tag" << std::endl;
>> > }
>> >
>> > template <typename T = std::string>
>> > static void create(const T & var) {
>> >     test(someclass_version_tag<std::char_traits<T>>::tag(),
>> > static_cast<someclass_version_tag<std::char_traits<T>>::type>(var));
>> > }
>> > };
>> >
>> > int main(int argc, char * argv[]) {
>> >     //wxString     tmp  = "wxString";
>> >     //foo::create<wxString>(tmp);
>> >     std::wstring tmp2 = L"wstring";
>> >     foo::create<std::wstring>(tmp2);
>> >     std::string tmp3 = "string";
>> >     foo::create<std::string>(tmp3);
>> >     return 0;
>> > }
>> >
>> >
>> >
>> > If I remove the comments in main, the complier will fail, so what is the
>> > correct way to mapping the type?
>> >
>> > Thanks in advance.
>> >
>> > Environment: VC++ 2013
>> >
>> > --
>> > Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>> >
>> > To unsubscribe, send email to [hidden email]
>> > or visit http://groups.google.com/group/wx-users
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to [hidden email]
> or visit http://groups.google.com/group/wx-users

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Boying Xu
Hi again
There are a series of two setup of core functions handles std::string and std::wstring in quite different way
My current implentation is using two function names like
void foo(std::string var) handls string
and
template<typename T>foo_w(T var) handls wstring and wxString
because I can do static_cast<std::wstring> T from std::wstring or wxString  without any problem
The code is running very smoothly.
But if to publish the code for more users, it would be nice to have same api to handle any type of string.  that means:       foo and template foo_w  -> template foo


Thanks.





On Wednesday, February 22, 2017 at 5:45:36 PM UTC+8, Igor Korot wrote:
Hi,

On Wed, Feb 22, 2017 at 3:08 AM, Boying Xu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DXNNpu19DAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">xubo...@...> wrote:
> Unfortunately, not
> First I would prefire to call static_cast<std::wstring> wxstr_var, instead
> of ToStdWstring(), the latter is pure run-time execution code and not exists
> in std::basic_strings
> The limitation here is the type must be detected during compiler time, so
> compiler can choose the proper overload function (test())
> static_cast still can not be properlly used at complier time.
> One ugly workaround is include <wx/wx.h> at the beginning of the header file
> and make a special tag mapping like this:

Ok.
Now I wonder why do you need to split between string and wstring?
Why not use std::wstring explicitly?

Thank you.

>
>
> template <>
> struct someclass_version_tag<std::char_traits<wxString>> {
>     typedef std::wstring type;
>     typedef v1_tag      tag;
> };
>
> However, this will limit my module be published with wx framework or I shall
> split the module for wx and non-wx users
> Additionally this will break the VC++ SDL check for some strange reason
>
> So I really looking for a traits version for overloading mechanism.
> The idea is from this post:
> <a href="https://stackoverflow.com/questions/4522375/switch-passed-type-from-template" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F4522375%2Fswitch-passed-type-from-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFYNPYWZJLUGAlqN091Mp_NElhUrQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F4522375%2Fswitch-passed-type-from-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFYNPYWZJLUGAlqN091Mp_NElhUrQ&#39;;return true;">https://stackoverflow.com/questions/4522375/switch-passed-type-from-template
>
>
>
> PS
> I also find a way called SFINAE (Substitution Failure Is Not An Error),
> however that way is a bit hack, complier version requied,  and going too
> far, which I don't really want to try it(getting into limbo):)
>
>
>
>
>
>
> On Wednesday, February 22, 2017 at 3:13:55 PM UTC+8, Igor Korot wrote:
>>
>> Hi,
>>
>> On Tue, Feb 21, 2017 at 10:15 PM, Boying Xu <[hidden email]> wrote:
>> > According to the online document
>> > <a href="https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Ffossies.org%2Fdox%2FwxWidgets-3.1.0%2FclasswxString.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEvOcKgB7fEsM2g3g7SDkkJpwXo6g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Ffossies.org%2Fdox%2FwxWidgets-3.1.0%2FclasswxString.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEvOcKgB7fEsM2g3g7SDkkJpwXo6g&#39;;return true;">https://fossies.org/dox/wxWidgets-3.1.0/classwxString.html
>> >
>> >> By default, wxString uses std::basic_string specialized for the
>> >> platform-dependent wchar_t type, meaning that it is not
>> >> memory-efficient for
>> >> ASCII strings, especially under Unix platforms where every ASCII
>> >> character,
>> >> normally fitting in a byte, is represented by a 4 byte wchar_t.
>> >
>> >
>> > This is very handy as in my code I can do
>> > static_cast<std::wstring>(wxString_type_var> and continue the process
>> > using
>> > the code applied to wchar_t
>> >
>> > Now my question is how I determinate the wxString, string, and wstring
>> > at
>> > complier time to get proper mapping to overloaded function for string
>> > and
>> > wstring
>> > I would prefer to map wxString, wstring -> version1 and string to
>> > version 2
>>
>> Can't you just call ToStdWstring()?
>>
>> Thank you.
>>
>> >
>> >
>> > #include <wx/wx.h>
>> >
>> > #include <iostream>
>> > #include <map>
>> > #include <memory>
>> > #include <typeindex>
>> > #include <typeinfo>
>> > #include <type_traits> // C++0x
>> >
>> > namespace foo {
>> >
>> > struct v1_tag {};
>> > struct v2_tag {};
>> >
>> > template <typename T>
>> > struct someclass_version_tag;
>> >
>> > template <>
>> > struct someclass_version_tag<std::char_traits<std::wstring>> {
>> >     typedef std::wstring type;
>> >     typedef v1_tag       tag;
>> > };
>> >
>> > template <>
>> > struct someclass_version_tag<std::char_traits<std::string>> {
>> >     typedef std::string type;
>> >     typedef v2_tag      tag;
>> > };
>> >
>> > void test(v1_tag, std::wstring & x) {
>> >     std::cout << "v1_tag" << std::endl;
>> > }
>> >
>> > void test(v2_tag, std::string & x) {
>> >     std::cout << "v2_tag" << std::endl;
>> > }
>> >
>> > template <typename T = std::string>
>> > static void create(const T & var) {
>> >     test(someclass_version_tag<std::char_traits<T>>::tag(),
>> > static_cast<someclass_version_tag<std::char_traits<T>>::type>(var));
>> > }
>> > };
>> >
>> > int main(int argc, char * argv[]) {
>> >     //wxString     tmp  = "wxString";
>> >     //foo::create<wxString>(tmp);
>> >     std::wstring tmp2 = L"wstring";
>> >     foo::create<std::wstring>(tmp2);
>> >     std::string tmp3 = "string";
>> >     foo::create<std::string>(tmp3);
>> >     return 0;
>> > }
>> >
>> >
>> >
>> > If I remove the comments in main, the complier will fail, so what is the
>> > correct way to mapping the type?
>> >
>> > Thanks in advance.
>> >
>> > Environment: VC++ 2013
>> >
>> > --
>> > Please read <a href="http://www.wxwidgets.org/support/mlhowto.htm" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;">http://www.wxwidgets.org/support/mlhowto.htm before posting.
>> >
>> > To unsubscribe, send email to [hidden email]
>> > or visit <a href="http://groups.google.com/group/wx-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;">http://groups.google.com/group/wx-users
>
> --
> Please read <a href="http://www.wxwidgets.org/support/mlhowto.htm" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.wxwidgets.org%2Fsupport%2Fmlhowto.htm\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGeLI38cNrlq2cC-jNsMzVrvhdIVA&#39;;return true;">http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="DXNNpu19DAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">wx-users+u...@googlegroups.com
> or visit <a href="http://groups.google.com/group/wx-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/wx-users&#39;;return true;">http://groups.google.com/group/wx-users

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Vadim Zeitlin-4
In reply to this post by Boying Xu
On Tue, 21 Feb 2017 19:15:58 -0800 (PST) Boying Xu wrote:

BX> This is very handy as in my code I can do
BX> static_cast<std::wstring>(wxString_type_var>

 Have you actually tested this? It really shouldn't compile. While wxString
does contain std::wstring as its first member, meaning that a cast like
this could be made to "work", for some very relaxed definition of the word
"work", you definitely shouldn't count on it working.

BX> Now my question is how I determinate the wxString, string, and wstring at
BX> complier time to get proper mapping to overloaded function for string and
BX> wstring
BX> I would prefer to map wxString, wstring -> version1 and string to version 2

 You will need to define a tag for wxString which will indeed make your
code dependent on wxString declaration, but this shouldn't be a problem in
practice, just take this specialization inside "#ifdef wxCHECK_VERSION" or
something like this.

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/

attachment0 (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Boying Xu
Hi VZ,
Nice to see you!!

The casting shall be OK, the complie is always passed and I have been using c style cast to string or wstring for years, the program is running quite robust(no crash, no leak, no unpredicted behaviour)

After some google, I finally figure out the way of handling this without include wx/wx.h in my source, here is the full code.



I have a feeling this C++ code is not what I was always using:), totally another stuff.....



#include <typeindex>
#include <typeinfo>
#include <string>
#include <iostream>

namespace foo {

struct v1_tag {};
struct v2_tag {};

template <typename T>
struct someclass_version_tag;

template <>
struct someclass_version_tag<std::true_type> {
   
typedef std::wstring type;
   
typedef v1_tag       tag;
};

template <>
struct someclass_version_tag<std::false_type> {
   
typedef std::string type;
   
typedef v2_tag      tag;
};

void test(v1_tag, std::wstring & x) {
    std
::cout << "v1_tag for wxstring and wxString" << std::endl;
}

void test(v2_tag, std::string & x) {
    std
::cout << "v2_tag for string" << std::endl;
}

template <typename T = std::string>
static void create(const T & var) {
    test
(someclass_version_tag<std::is_constructible<std::wstring, T>::type>::tag(), static_cast<someclass_version_tag<std::is_constructible<std::wstring, T>::type>::type>(var));
}
};

#include <wx/wx.h>
#include <map>

int main(int argc, char * argv[]) {
    wxString tmp
= "wxString";
    std
::cout << "testing wxString....   ";
    foo
::create<wxString>(tmp);
    std
::cout << "testing wstring....   ";
    std
::wstring tmp2 = L"wstring";
    foo
::create<std::wstring>(tmp2);
    std
::cout << "testing string....   ";
    std
::string tmp3 = "string";
    foo
::create<std::string>(tmp3);
   
return 0;
}






On Thursday, February 23, 2017 at 6:29:09 AM UTC+8, Vadim Zeitlin wrote:
On Tue, 21 Feb 2017 19:15:58 -0800 (PST) Boying Xu wrote:

BX> This is very handy as in my code I can do
BX> static_cast<std::wstring>(wxString_type_var>

 Have you actually tested this? It really shouldn't compile. While wxString
does contain std::wstring as its first member, meaning that a cast like
this could be made to "work", for some very relaxed definition of the word
"work", you definitely shouldn't count on it working.

BX> Now my question is how I determinate the wxString, string, and wstring at
BX> complier time to get proper mapping to overloaded function for string and
BX> wstring
BX> I would prefer to map wxString, wstring -> version1 and string to version 2

 You will need to define a tag for wxString which will indeed make your
code dependent on wxString declaration, but this shouldn't be a problem in
practice, just take this specialization inside "#ifdef wxCHECK_VERSION" or
something like this.

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               <a href="http://www.tt-solutions.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.tt-solutions.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhPHTZbdYZYM-AqcnZXykG1ueWhw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.tt-solutions.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhPHTZbdYZYM-AqcnZXykG1ueWhw&#39;;return true;">http://www.tt-solutions.com/

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re[2]: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Vadim Zeitlin-4
On Wed, 22 Feb 2017 19:27:05 -0800 (PST) Boying Xu wrote:

BX> The casting shall be OK, the complie is always passed and I have been using
BX> c style cast to string or wstring for years, the program is running quite
BX> robust(no crash, no leak, no unpredicted behaviour)

 Oops, yes, you're right, I didn't realize that the implicit conversion of
wxString to "wchar_t*" was sufficient for static_cast<> of wxString to
std::wstring to work because std::wstring also has an implicit ctor from
"wchar_t*". I thought the use of 2 user-defined conversions would prevent
it from working, but the transformation of "static_cast<T>(e)" to "T x{e}"
doesn't count as a user-defined conversion, so I was wrong, sorry.

 I still think writing static_cast<std::wstring>(wxs) is a rather confusing
(and possibly less efficient) way of spelling wxs.ToStdWstring(), but, at
least, it is indeed well-defined and does work.

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/

attachment0 (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: How to simplify wxString type to make it same as std::wstring(wchar_t) in c++11

Boying Xu
Hi again
I just noticed the
std::is_constructible<std::wstring, T>::type so far only works in VC++(2013), the same code don't compile in g++, and even in VC++, it's still not supporting return type tag.
So I still need to so some research on it.
Thanks for your help!


On Friday, February 24, 2017 at 9:36:55 AM UTC+8, Vadim Zeitlin wrote:
On Wed, 22 Feb 2017 19:27:05 -0800 (PST) Boying Xu wrote:

BX> The casting shall be OK, the complie is always passed and I have been using
BX> c style cast to string or wstring for years, the program is running quite
BX> robust(no crash, no leak, no unpredicted behaviour)

 Oops, yes, you're right, I didn't realize that the implicit conversion of
wxString to "wchar_t*" was sufficient for static_cast<> of wxString to
std::wstring to work because std::wstring also has an implicit ctor from
"wchar_t*". I thought the use of 2 user-defined conversions would prevent
it from working, but the transformation of "static_cast<T>(e)" to "T x{e}"
doesn't count as a user-defined conversion, so I was wrong, sorry.

 I still think writing static_cast<std::wstring>(wxs) is a rather confusing
(and possibly less efficient) way of spelling wxs.ToStdWstring(), but, at
least, it is indeed well-defined and does work.

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               <a href="http://www.tt-solutions.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.tt-solutions.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhPHTZbdYZYM-AqcnZXykG1ueWhw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.tt-solutions.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhPHTZbdYZYM-AqcnZXykG1ueWhw&#39;;return true;">http://www.tt-solutions.com/

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users