From: David A. <da...@bo...> - 2004-09-27 13:45:47
|
Stefan Seefeld <sse...@ar...> writes: > Hi there, > > I'm working on a constant expression analyzer > to assist the C++ parser. It's working nicely > so far. Here is what it already can deal with: > > http://synopsis.fresco.org/viewsvn/synopsis-Synopsis/trunk/tests/OpenCxx/Con > stEvaluator/ > > However, some cases are not as simple, and so > I'd appreciate feedback: the 'sizeof' operator > can be used in const expressions, and so I have > to deal with it. > AFAIK each compiler has its own memory > layout, and thus there's no standard algorithm > to determine the size of an arbitrary type. > > On the other hand, there is no requirement for > Synopsis / OpenC++ to be binary compatible with > any other compiler, so may be this is a non-issue. It's still an issue. There are some things that are constant: sizeof(char) == 1 sizeof(char[2]) == 2 sizeof(char(&)[2]) == 2 sizeof(char[3]) == 3 sizeof(X[N]) == N * sizeof(X) There are programs that rely on these relationships. > Still, I need to provide some numbers when asked > for the size of an object. Any ideas how to approach > this ? Any references are highly appreciated ! > > Regards, > Stefan You're going to need to build in full overload resolution and template instantiation capability, which is no small job. consider: template <class T> typename something<T>::type f(T&); char f(...); sizeof(f(g())); If g returns a value, the result is 1. If g returns a reference, you'll call the first f, and you'll instantiate something<T> to find out its return type. Before you object, this sort of thing is very common in the Boost libraries. Even though it's a big job, it would be a great service to the C++ community. I could probably find people to help you with it. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com |