Re: C++ equivalent of C FAMs and subsequent issues with offsetof
In article <slrnf6l0jq.moh.usenetspam01@fermat.hashpling.org>, Charles
Bailey <usenetspam01@hashpling.org> wrote:
I appreciate that considering all corner cases and constructing
unambiguous and clear wording for a standard would be a lot of effort
so it would take considerable collective desire for this feature to be
considered but I would definitely be in favour of it. It would be a
shame for C and C++ to diverge more than necessary.
Seems like a lot of work compared to merely providing in global
namespace when this problem occurs, as the most difficult of the
extern "C" functions are already [hopefully] written in the supporting
C code. The getters are one liners if they don't already exist and
need to be compiled in C, if missing.
extern "C"
{
struct foo;
int get_len_from foo(foo *);
double *get_data_froo(foo *);
foo * create_foo(int);
destroy_foo(foo *);
}
std::tr1::shared_ptr<foo> CreateFoo(int len)
{
return std:shared_ptr<foo>(create_foo(len),destroy_foo);
}
or better yet contain the shared_ptr in a genuine C++ class
providing access to the C functions and opague struct.
class cpp_foo
{
std::tr1::shared_ptr<foo> p_foo;
public:
explicit cpp_foo(int n):p_foo(create_foo(n),destroy_foo){}
int len() const {return get_len_from_foo(p_foo.get());}
double & operator [] (int n)
{return *(get_data_from_foo(p_foo.get())+n);}
double *data() { return get_data_from_foo(p_foo.get());}
operator foo *() { return p_foo.get();}// for easy calling of C
};
or something similiar.
This handles exceptions well, and automatic correct deletions using
C++ idioms and is easy to impliment and will be accepted C++09 standard
as of now. [requires shared_ptr from boost or tr1 presently]
Given this kind of workaround for this problem and
other more pressing considerations I stiil doubt a change, but who
knows. I do know that no proposal means no change.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]