Re: [boost] Re: Hello MPL world!
At 10:17 PM 1/6/2003, Howard Hinnant wrote: On Monday, January 6, 2003, at 06:50 PM, David Abrahams wrote: OK, I see your point. How about: template class T struct my_container : if_ and_ is_pointerT , is_PODremove_pointerT , impl1 , impl2 ::type { ... }; Or maybe even just: template class T struct my_container : if_is_PODT::value, impl1, impl2::type { ... }; These are the examples that resonate with me, particularly Howard's version. It looks so easy, and has obvious practical uses. It would motivate me. But it needs to be a complete, compilable, runable, program so I can try it, modify it, etc. --Beman ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
Howard Hinnant [EMAIL PROTECTED] writes: On Saturday, January 11, 2003, at 09:42 AM, Beman Dawes wrote: Or maybe even just: template class T struct my_container : if_is_PODT::value, impl1, impl2::type { ... }; These are the examples that resonate with me, particularly Howard's version. It looks so easy, and has obvious practical uses. It would motivate me. But it needs to be a complete, compilable, runable, program so I can try it, modify it, etc. Despite the fact that I suggested it, I don't find it a killer example. ;-) I'm not sure we're looking for one. Is the classic hello, world a killer example of why traditional C++ is cool? I don't think so. On the other hand, if you've never seen C++, you can get a feeling for what it's like to work with the language from looking at hello, world. I already code stuff like this, with this other pattern: snip simple metaprogramming example I'm not convinced that the version using if_ is significantly simpler. :-\ I don't think we're trying to beat that example by very much. The person already familiar with metaprogramming won't find much to be impressed with, unless they look very closely, in which case it will only be slightly intriguing. I think that's more than you can say for classic hello, world. -Dave -- David Abrahams [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
On Saturday, January 11, 2003, at 09:42 AM, Beman Dawes wrote: Or maybe even just: template class T struct my_container : if_is_PODT::value, impl1, impl2::type { ... }; These are the examples that resonate with me, particularly Howard's version. It looks so easy, and has obvious practical uses. It would motivate me. But it needs to be a complete, compilable, runable, program so I can try it, modify it, etc. Despite the fact that I suggested it, I don't find it a killer example. ;-) I already code stuff like this, with this other pattern: template class T, bool struct impl; templateclass T struct implT, true { }; // impl1, optimized templateclass T struct implT, false { }; // impl2, not optimized template class T struct my_container : private implT, is_PODT::value { ... }; I'm not convinced that the version using if_ is significantly simpler. :-\ templateclass T struct impl1 { }; // optimized templateclass T struct impl2 { }; // not optimized template class T struct my_container : private if_is_PODT, impl1T, impl2T ::type { ... }; if_ really shines when it is inconvenient to partially specialize an entire class just to get one little different type: template class T struct some_example { typedef typename if_some_conditionT::value, type1, type2::type the_type; ... }; Good example: http://www.cuj.com/experts/1901/austern.htm?topic=experts -Howard ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
RE: [boost] Re: Hello MPL world!
-Original Message- From: David B. Held [mailto:[EMAIL PROTECTED]] Sent: Monday, January 06, 2003 8:14 PM To: [EMAIL PROTECTED] Subject: [boost] Re: Hello MPL world! While this is a cute idea, my first impression would be: Uh...is this really something I could use in my own code? On the other hand, I seem to use compile-time if more than anything else, even in user code. I suspect that most people will use mpl::if_ and type traits more than anything else, so I think Dave's original example with is_pointer would connect with the most programmers. On the other hand, I suspect that library authors are more likely to use the type containers and algorithms, so an example illustrating those might be more appropriate for them. So I guess it depends on the intended audience. P.S. Outputting Hello, world in a way that generates significantly more code than the run-time version is probably not a good way to endear users to metaprogramming. ; The original version I suggested used fold, and for that, you do need to use if_ since you need to determine whether to call Prev::eval(). I didn't post my implementation since it took me quite a while to get it working with VC6.5 and I thought only the general concepts were necessary, not a full blown example. In any case, I think just using if_ and type traits lacks the small rush of excitement when you see it work. Regarding the much more code, metaprogramming is used to produce a lot of inline code in Blitz style templates. This is considered a type of optimization, mainly in speed. If there weren't a cout.write, I think my example would work faster than the equivalent of using streaming operators . As for usefulness, I agree that the next thought after the rush of excitement, is so what? I could argue that doing for_each on the vector_c is not different than doing for_each on a compile time vector of types. In fact, the functor receives a type, not a character. Generation of long sequences of code is one use of metaprogramming. A vector of pairs, might be used to implement read serialization of a discriminated union. This is much more useful, whether for a library author or not, but much more complex for a beginning example to understand: list pairint_c0, int, pairint_c1, double, pairint_c2, string int discriminator; stream discriminator; varianttransformtypelist,select2nd::type v; for_eachtypelist, identity(serialize_if_appropriate(discriminator, stream, v)); with the following definition: struct serialize_if_appropriate { public: ... template class T void operator()(T) const { if (T::first::value == discriminator) { T::second::type t; stream t; v = t; } } private: istream stream; int discriminator; varianttypelist v; }; ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
Yitzhak Sapir [EMAIL PROTECTED] writes: The original version I suggested used fold, and for that, you do need to use if_ since you need to determine whether to call Prev::eval(). I didn't post my implementation since it took me quite a while to get it working with VC6.5 and I thought only the general concepts were necessary, not a full blown example. In any case, I think just using if_ and type traits lacks the small rush of excitement when you see it work. Yeah, it doesn't do very well in that department. Regarding the much more code, metaprogramming is used to produce a lot of inline code in Blitz style templates. This is considered a type of optimization, mainly in speed. If there weren't a cout.write, I think my example would work faster than the equivalent of using streaming operators . Dave's mention of efficiency was the weakest part of the argument, but significant nonetheless. As for usefulness, I agree that the next thought after the rush of excitement, is so what? I could argue that doing for_each on the vector_c is not different than doing for_each on a compile time vector of types. In fact, the functor receives a type, not a character. Yep. I'm nervous about showing iteration and sequences in a this example, though. There's nothing that sophisticated going on in hello, world. Generation of long sequences of code is one use of metaprogramming. A vector of pairs, might be used to implement read serialization of a discriminated union. This is much more useful, whether for a library author or not, but much more complex for a beginning example to understand: ... Sure, it's comparitively easy to come up with useful examples that are more complicated. Incidentally, yours needs some tweaks for generality. The main reason is that for_each tries to value-initialize an instance of each of the types it processes and passes that to the function object. If the type isn't default-constructible, it won't compile. I'm not sure that preserving this property is worthwhile. Opinions, Aleksey? typedef list pairint_c0, int , pairint_c1, double , pairint_c2, string typelist; int discriminator; stream discriminator; varianttransformtypelist,select2nd::type v; for_eachtypelist, wrap(serialize_if_appropriate(discriminator, stream, v)); with the following definition: struct serialize_if_appropriate { public: ... template class W void operator()(W) const { typedef W::type T; // -- if (T::first::value == discriminator) { T::second::type t; stream t; v = t; } } private: istream stream; int discriminator; varianttypelist v; }; -- David Abrahams [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
At 05:09 PM 1/6/2003, David B. Held wrote: David Abrahams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Howard Hinnant [EMAIL PROTECTED] writes: OK, I see your point. How about: template class T struct my_container : if_ and_ is_pointerT , is_PODremove_pointerT , impl1 , impl2 ::type { ... }; I think if you provided a copy c'tor that called memcpy() for impl1, or some other POD optimization, that would motivate the example for people who might be a little slow on the uptake (like I am sometimes!). Otherwise, it seems like a decent introduction. You might even be a little suggestive and call it my_vector, to imply that you are creating a POD-optimized vector (which people should be able to relate to easily enough). I like this suggestion a lot. A simple, useful example that can't easily be done without something like MPL. ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
Alisdair Meredith [EMAIL PROTECTED] writes: Terje Slettebø wrote: The results don't necessarily have to be printed out at compile-time (Erwin Unruh printed the results using compiler-warnings, but that is of course highly implementation dependent). Hello, world in compile-time programming doesn't necessarily have to be the same kind of program as in run-time programming, since the way it works is different. I suspect a metaprogram that does nothing but issue a few diagnostics would be dismissed as a curiosity, maybe a cool toy, but hardy seen as an interesting tool by someone new to the idea. Creating a static array of size 5! shows a little more potential, but what to do with the array? And that's still primarily numerical computation. However, it is far too easy to be negative when I have nothing better to offer in return (although I will be quite keen to see the final example, MPL is one of those items on my 'to-do' list that never quite hits the top) My current working idea is really simple: struct impl1 { ... }; struct impl2 { ... }; template class T struct my_container : if_is_pointerT, impl1, impl2::type { ... }; This is something I want to do all the time; it lets you swap out a completely different implementation for the class (well, except for the constructors) based on type properties. It also shows the convenient interaction of type_traits metafunctions with MPL algorithms. Can we do better? What are people actively doing with MPL anyway? Perhaps and answer to that question will indicate what 'Hello world' should look like. It should be more than a neat exercise in syntax, it should demonstrate the idea of real-world use. Currently I mainly use compile-time techniques for static assertions. It's a good thing to use MPL for, but I suspect the idea of causing errors isn't going to entice most people to dig deeper ;-) I have written some toy curiousities such as the classic factorial generator and recursive array-like containers. I am yet to make the leap from playing with the syntax to applying it to real world problems, and if we can come up with a simple example that makes that leap, we'll have hit gold [where 'we' here clearly means 'you' g] That's what we're interested in. My favorite examples are much too involved to be used for hello, world. -- David Abrahams [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
David B. Held [EMAIL PROTECTED] writes: David Abrahams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Yitzhak Sapir [EMAIL PROTECTED] writes: I think storing the text Hello world! in a vector [...] And then using functors to print it such as: [...] While this is a cute idea, my first impression would be: Uh...is this really something I could use in my own code? On the other hand, I seem to use compile-time if more than anything else, even in user code. I suspect that most people will use mpl::if_ and type traits more than anything else, so I think Dave's original example with is_pointer would connect with the most programmers. I think I agree with you here. On the other hand, I suspect that library authors are more likely to use the type containers and algorithms, so an example illustrating those might be more appropriate for them. So I guess it depends on the intended audience. I dunno, I use compile-time if_ in my libraries more than anything else. Dave P.S. Outputting Hello, world in a way that generates significantly more code than the run-time version is probably not a good way to endear users to metaprogramming. ; I guess you have a point there, too. -- David Abrahams [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
From: Alisdair Meredith [EMAIL PROTECTED] Terje Slettebø wrote: The results don't necessarily have to be printed out at compile-time (Erwin Unruh printed the results using compiler-warnings, but that is of course highly implementation dependent). Hello, world in compile-time programming doesn't necessarily have to be the same kind of program as in run-time programming, since the way it works is different. I suspect a metaprogram that does nothing but issue a few diagnostics would be dismissed as a curiosity, maybe a cool toy, but hardy seen as an interesting tool by someone new to the idea. Unlike the traditional Hello, world? A Hello, world programs isn't meant to show the advantage of C++, but to be a complete and minimal program that does something interesting, so that you may get a gentle introduction to the language, and you can see how programs in the language look, and also check that the system (compiler, etc.) is set up properly. The absolute minimal program is of course main() {}, but as it doesn't do anything, it isn't very interesting. There was a discussion a long time ago in Communications of the ACM, about improving Hello, world, which meant making it more complex. As Kevlin Henney said about it, it missed the point completely, as it's supposed to be a complete, minimal program, that does something interesting. To this end, I think simple programs, like the ones mentioned, may fit. It may also include doing something useful, such as your suggestion, here. Creating a static array of size 5! shows a little more potential, but what to do with the array? Filling it with all permutations of the numbers 1-5? However, it is far too easy to be negative when I have nothing better to offer in return (although I will be quite keen to see the final example, MPL is one of those items on my 'to-do' list that never quite hits the top) What are people actively doing with MPL anyway? I'm using mpl::if_ to simulate partial specialisation in the lexical_cast proposition. It used to use boost/pending/ct_if.hpp, but as MPL is now of Boost, I've used that. (The version in the Files section still uses ct_if, though, as it hasn't been updated.) So it's hardly groundbreaking things, :) and it could be done earlier, too. However, simple components like if_ is often useful, for the same reason that simple components like Boost.Ref and Boost.Bind is useful - they do one thing well. Perhaps and answer to that question will indicate what 'Hello world' should look like. It should be more than a neat exercise in syntax, it should demonstrate the idea of real-world use. I think it's a good idea to get real-world examples of metaprogramming. However, that may not be much like Hello, world, as also Dave Abrahams mention in another posting. You can of course have both simple and more complex examples. Regards, Terje ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
On Monday, January 6, 2003, at 01:14 PM, David B. Held wrote: While this is a cute idea, my first impression would be: Uh...is this really something I could use in my own code? On the other hand, I seem to use compile-time if more than anything else, even in user code. I suspect that most people will use mpl::if_ and type traits more than anything else, so I think Dave's original example with is_pointer would connect with the most programmers. On the other hand, I suspect that library authors are more likely to use the type containers and algorithms, so an example illustrating those might be more appropriate for them. So I guess it depends on the intended audience. I would be careful that your target audience doesn't look at the helloworld and say, gee, why don't I just use existing language features: template class T struct my_container { // impl2 ... }; template class T struct my_containerT* { // impl1 ... }; -Howard ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
On Monday, January 6, 2003, at 06:50 PM, David Abrahams wrote: OK, I see your point. How about: template class T struct my_container : if_ and_ is_pointerT , is_PODremove_pointerT , impl1 , impl2 ::type { ... }; Or maybe even just: template class T struct my_container : if_is_PODT::value, impl1, impl2::type { ... }; -Howard ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
- Original Message - From: David Abrahams [EMAIL PROTECTED] David B. Held [EMAIL PROTECTED] writes: David Abrahams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Yitzhak Sapir [EMAIL PROTECTED] writes: I think storing the text Hello world! in a vector [...] And then using functors to print it such as: [...] While this is a cute idea, my first impression would be: Uh...is this really something I could use in my own code? On the other hand, I seem to use compile-time if more than anything else, even in user code. I suspect that most people will use mpl::if_ and type traits more than anything else, so I think Dave's original example with is_pointer would connect with the most programmers. I think I agree with you here. And I disagree. I think the hello world example presented is a good start to whet the appetite of a would be MPL user. Why? 1) It shows, in simple terms, how compile time structures can be useful at runtime. 2) An STL programmer will immediately be at home because of the use of the vector and the for_each. On the other hand, I suspect that library authors are more likely to use the type containers and algorithms, so an example illustrating those might be more appropriate for them. So I guess it depends on the intended audience. I dunno, I use compile-time if_ in my libraries more than anything else. Same here. P.S. Outputting Hello, world in a way that generates significantly more code than the run-time version is probably not a good way to endear users to metaprogramming. ; Again, I disagree. The C++ hello world generates more code (bringing in the iostreams in whole) than its C counterpart yet no one complained. Joel de Guzman [EMAIL PROTECTED] http://www.boost-consulting.com ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
At 08:19 PM 1/6/2003, Joel de Guzman wrote: - Original Message - From: David Abrahams [EMAIL PROTECTED] ... P.S. Outputting Hello, world in a way that generates significantly more code than the run-time version is probably not a good way to endear users to metaprogramming. ; Again, I disagree. The C++ hello world generates more code (bringing in the iostreams in whole) than its C counterpart yet no one complained. Oh yes we did! ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Hello MPL world!
Howard Hinnant [EMAIL PROTECTED] writes: On Monday, January 6, 2003, at 06:50 PM, David Abrahams wrote: Or maybe even just: template class T struct my_container : if_is_PODT::value, impl1, impl2::type ^^^ you'd have to drop that part (yes, it's magic wink). { ... }; OK, yeah, that has promise. -- David Abrahams [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost