Re: [boost] Re: How to convert a template parameter into a string
- Original Message - From: "Peter Dimov" <[EMAIL PROTECTED]> To: "Boost mailing list" <[EMAIL PROTECTED]> Sent: Monday, March 03, 2003 7:59 AM Subject: Re: [boost] Re: How to convert a template parameter into a string > Robert Allan Schwartz wrote: > >>> I believe a "standardized" (within Boost), portable, and *readable* > >>> text representation of T makes my proposal better than typeid(). > >> > >> I think if readability is the main criterion we'd do much better to > >> invest in decoding the typeids generated by GCC. I believe there's > >> even a library that comes with it that does that job. > > > > Why should millions of programmers have to make that "invest"ment? > > > > Wouldn't it be better if gcc simply generated better type names? > > Sure, if there existed an unambiguous definition of "better type name". Maybe there's no "perfect" definition of "better", but let's see how far we can go with a "good-enough" definition. T is P4base T is 4base T is PC4base T is 4base when compiled by g++ and executed in cygwin, is "not good enough". Anyone disagree? T is class base * T is class base T is class base const * T is class base when compiled and executed by MSVC 6.0, is "good enough", but it could be "better" if it eliminated the redundant "class" specifier. We could argue about the difference between "base const *" and "base const*" (i.e. whitespace). We could argue about the difference between "base const" and "const base". But I still think we should fix the gap in the Standard, and choose standard, portable, type names. If we don't do that, then at least my proposal lets each developer choose what THEY want for type names. Robert ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
Robert Allan Schwartz wrote: >>> I believe a "standardized" (within Boost), portable, and *readable* >>> text representation of T makes my proposal better than typeid(). >> >> I think if readability is the main criterion we'd do much better to >> invest in decoding the typeids generated by GCC. I believe there's >> even a library that comes with it that does that job. > > Why should millions of programmers have to make that "invest"ment? > > Wouldn't it be better if gcc simply generated better type names? Sure, if there existed an unambiguous definition of "better type name". ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
"Robert Allan Schwartz" <[EMAIL PROTECTED]> writes: >> > I believe a "standardized" (within Boost), portable, and *readable* text >> > representation of T makes my proposal better than typeid(). >> >> I think if readability is the main criterion we'd do much better to >> invest in decoding the typeids generated by GCC. I believe there's >> even a library that comes with it that does that job. > > Why should millions of programmers have to make that "invest"ment? They don't. > Wouldn't it be better if gcc simply generated better type names? > > That's one fix, not millions. Since as I said, they supply a library which does it with the compiler, I don't think it's really as bad as you make out. -- Dave Abrahams Boost Consulting www.boost-consulting.com ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
> I think that is a problem too. Maybe a macro could make things easier: > > #define DECLARE_SPELLABLE( a_type ) \ > template<> class spelling { public: static const std::string > result; }; \ > const std::string spelling::result = #a_type > > but this should be preferrable be put in headers, so why not make the static > data into a function instead? > Like this: > > template< typename T > > class spelling > { > public: > const string& result() const >{ >static const string res = ..; >return res; >} > }; > > And the macro could be changed accordingly. Looks good to me. Robert ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
> > I believe a "standardized" (within Boost), portable, and *readable* text > > representation of T makes my proposal better than typeid(). > > I think if readability is the main criterion we'd do much better to > invest in decoding the typeids generated by GCC. I believe there's > even a library that comes with it that does that job. Why should millions of programmers have to make that "invest"ment? Wouldn't it be better if gcc simply generated better type names? That's one fix, not millions. Robert ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] Re: How to convert a template parameter into a string
"Dirk Gerrits" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] [snip] > At what cost? The strings you generate are indeed very readable, but > specializing spelling for every type that might be a template parameter > someday seems like too much of a burden to me. > > Others might disagree though... I think that is a problem too. Maybe a macro could make things easier: #define DECLARE_SPELLABLE( a_type ) \ template<> class spelling { public: static const std::string result; }; \ const std::string spelling::result = #a_type but this should be preferrable be put in headers, so why not make the static data into a function instead? Like this: template< typename T > class spelling { public: const string& result() const { static const string res = ..; return res; } }; And the macro could be changed accordingly. regards Thorsten ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] Re: How to convert a template parameter into a string
Robert Allan Schwartz wrote: Perhaps my spelling class template could be folded into type_traits? [code snipped] Looks interesting, but I'm not sure if it's such a huge advantage over: template void foo(T) { cout << "T is " << typeid(T).name() << endl; } The resulting string of your method is more portable of course, but is that the only reason? No. There are other reasons why typeid() is not as "good" as my proposal: 1) You must #include in order to use typeid(). This seems to me to be unnecessary overhead. But wouldn't your method need an #include "spelling.hpp"? 2) The following program: class base { }; class derived : public base { }; int main(void) { base * b = new derived; base const * const b2 = new derived; foo(b); foo(*b); foo(b2); foo(*b2); return 0; } produces: T is P4base T is 4base T is PC4base T is 4base when compiled by g++ and executed in cygwin. > As you point out, the string returned by typeid().name() is not specified by the Standard, so it is not portable, but in this case, it is extremely difficult to decipher. Ouch. That looks horrible indeed. I believe a "standardized" (within Boost), portable, and *readable* text representation of T makes my proposal better than typeid(). At what cost? The strings you generate are indeed very readable, but specializing spelling for every type that might be a template parameter someday seems like too much of a burden to me. Others might disagree though... Dirk Gerrits ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
"Robert Allan Schwartz" <[EMAIL PROTECTED]> writes: > I believe a "standardized" (within Boost), portable, and *readable* text > representation of T makes my proposal better than typeid(). I think if readability is the main criterion we'd do much better to invest in decoding the typeids generated by GCC. I believe there's even a library that comes with it that does that job. -- Dave Abrahams Boost Consulting www.boost-consulting.com ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: How to convert a template parameter into a string
> > Perhaps my spelling class template could be folded into type_traits? > > [code snipped] > > Looks interesting, but I'm not sure if it's such a huge advantage over: > > template > void foo(T) > { > cout << "T is " << typeid(T).name() << endl; > } > > The resulting string of your method is more portable of course, but is > that the only reason? No. There are other reasons why typeid() is not as "good" as my proposal: 1) You must #include in order to use typeid(). This seems to me to be unnecessary overhead. 2) The following program: class base { }; class derived : public base { }; int main(void) { base * b = new derived; base const * const b2 = new derived; foo(b); foo(*b); foo(b2); foo(*b2); return 0; } produces: T is P4base T is 4base T is PC4base T is 4base when compiled by g++ and executed in cygwin. As you point out, the string returned by typeid().name() is not specified by the Standard, so it is not portable, but in this case, it is extremely difficult to decipher. I believe a "standardized" (within Boost), portable, and *readable* text representation of T makes my proposal better than typeid(). Thanks for your feedback! Robert ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] Re: How to convert a template parameter into a string
Robert Allan Schwartz wrote: The attached document was submitted for publication to C/C++ Users Journal today, but I thought it might be worth submitting to Boost as well. Perhaps my spelling class template could be folded into type_traits? [code snipped] Looks interesting, but I'm not sure if it's such a huge advantage over: template void foo(T) { cout << "T is " << typeid(T).name() << endl; } The resulting string of your method is more portable of course, but is that the only reason? Regards, Dirk Gerrits ___ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost