Re: [boost] [optional] generates unnessesary code for trivial types

On 1.2.2012. 17:53, Hite, Christopher wrote:
As for the debugger the new C++ allows for a union to contain a class. So if a placeholder implemention using such a union would show the data in debug.
But the pointer approach would also work with "real world" compilers ;)
This isn't for science fiction. Gcc says they support n2544 since 4.6. http://gcc.gnu.org/projects/cxx0x.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2544.pdf I just tried it out: template<typename T> union placeholder{ T v; placeholder() {} ~placeholder (){} }; ... placeholder<std::string> ps; std::string s("hi"); new(&ps.v) std::string(s); < Uninited happens to contain null. > (gdb) print ps $2 = {v = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x0}}} < Initied: > (gdb) print ps $3 = {v = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x602028 "hi"}}} I don't know what platform you're using, but it seems completely reasonable to #ifdef a version of the placeholder in that does this so people can debug easily. Does that remove the main reason you wanted a pointer? BTW I did look at your code some. The deconstructor mixin, it's the same technique I used! For the ctor I just define a intermediate class with either default copy and operator= or impled ones. Chris
participants (1)
-
Hite, Christopher