
Hi, I was grep'ing for my name in the boost repository in order to update any old license reference and found the following definition in implicit_cast.hpp: // Macro for when you need a constant expression (Gennaro Prota) #define BOOST_IMPLICIT_CAST(dst_type, expr) \ ( sizeof( implicit_cast<dst_type>(expr) ) \ , \ static_cast<dst_type>(expr) \ ) This wasn't the form I submitted though, as the comma operator isn't allowed in a constant expression and I remember pointing out on this list that this was a case where it would be useful :) Maybe the above was simply copied from this post: http://lists.boost.org/MailArchives/boost/msg42102.php where I was saying that it *doesn't* work and that I had to use the check_helper<> template. Genny.

Gennaro Prota <gennaro_prota@yahoo.com> writes:
Hi,
I was grep'ing for my name in the boost repository in order to update any old license reference and found the following definition in implicit_cast.hpp:
// Macro for when you need a constant expression (Gennaro Prota) #define BOOST_IMPLICIT_CAST(dst_type, expr) \ ( sizeof( implicit_cast<dst_type>(expr) ) \ , \ static_cast<dst_type>(expr) \ )
This wasn't the form I submitted though, as the comma operator isn't allowed in a constant expression and I remember pointing out on this list that this was a case where it would be useful :) Maybe the above was simply copied from this post:
http://lists.boost.org/MailArchives/boost/msg42102.php
where I was saying that it *doesn't* work and that I had to use the check_helper<> template.
So, should I just remove it? Looking back at that thread, I'm surprised I included it in this header because I don't think I ever really understood what you were saying. I don't think there's an alternative to using the comma operator that will work both when dst_type is a dependent type in a template and when it is not dependent, because of differences in the need for "typename". -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

On Sun, 16 May 2004 10:40:52 -0400, David Abrahams <dave@boost-consulting.com> wrote:
So, should I just remove it?
Well, not because it isn't implementable. Admittedly, it isn't the most useful thing in the world either, so I don't have a strong opinion about its inclusion ;)
Looking back at that thread, I'm surprised I included it in this header because I don't think I ever really understood what you were saying.
I don't think there's an alternative to using the comma operator that will work both when dst_type is a dependent type in a template and when it is not dependent, because of differences in the need for "typename".
Good point. Then we could attack the expression part of static_cast. What about using the conditional operator? I don't know how that interacts with broken compilers, though. If you like it, I could provide the tests. //------------------------------------------------------------ #include <cstddef> namespace boost { template< typename T> struct identity { typedef T type; }; // The use of identity creates a non-deduced form, so that the // explicit template argument must be supplied template <typename T> inline T implicit_cast (typename identity<T>::type x) { return x; } } // namespace boost // Macro for when you need a constant expression. // Note that 'expr' appears three times but is // evaluated (at most) once. #define BOOST_IMPLICIT_CAST(dest_type, expr) \ (static_cast<dest_type>( \ sizeof(boost::implicit_cast<dest_type>(expr)) ? \ (expr) : (expr) \ ) \ ) //------------------------------------------------------------ // Examples: template <typename T> struct X { typedef T type; }; // integral constant expressions template <typename T> void f(T arg) { // dest_type is dependent int x [ BOOST_IMPLICIT_CAST(typename X<T>::type, 'A') ]; // dest_type is non-dependent int y [ BOOST_IMPLICIT_CAST(int, 'B') ]; } // constant-expressions for the purpose of non-local static object initialization // (a) null pointer value (of type char*) static void * pc = BOOST_IMPLICIT_CAST(void*, (char*)(0) ); // (b) null member pointer value struct X2 { void f(int) {} }; static void (X2::*pm)(int) = BOOST_IMPLICIT_CAST( void (X2::*)(int), 0); // (c) arithmetic constant expression static int n = BOOST_IMPLICIT_CAST(int, 1.0); // (d) address constant expression static const void * pso = BOOST_IMPLICIT_CAST(const void*, (const char*) (1 + &"hello")); // (e) reference constant expression static char c = 'x'; static int n2 = BOOST_IMPLICIT_CAST(int, c); // these should fail #ifdef TEST_FAILURES struct B {}; struct D : B {}; D d; B & ref = d; enum my_enum { value1, value2 }; void g() { BOOST_IMPLICIT_CAST(void, 1); BOOST_IMPLICIT_CAST(D&, ref); BOOST_IMPLICIT_CAST(my_enum, 1); } #endif int main() { f(1); f(8UL); } Genny.

Gennaro Prota <gennaro_prota@yahoo.com> writes:
On Sun, 16 May 2004 10:40:52 -0400, David Abrahams <dave@boost-consulting.com> wrote:
So, should I just remove it?
Well, not because it isn't implementable.
Sorry, I can't parse that. Should I remove it or not? Is it implementable? Maybe your code below answers those questions...
Admittedly, it isn't the most useful thing in the world either, so I don't have a strong opinion about its inclusion ;)
Looking back at that thread, I'm surprised I included it in this header because I don't think I ever really understood what you were saying.
I don't think there's an alternative to using the comma operator that will work both when dst_type is a dependent type in a template and when it is not dependent, because of differences in the need for "typename".
Good point. Then we could attack the expression part of static_cast. What about using the conditional operator? I don't know how that interacts with broken compilers, though. If you like it, I could provide the tests.
I like it. Maybe it's time to document this stuff, too ? ;-) -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

On Sat, 22 May 2004 13:11:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
Gennaro Prota <gennaro_prota@yahoo.com> writes:
On Sun, 16 May 2004 10:40:52 -0400, David Abrahams <dave@boost-consulting.com> wrote:
So, should I just remove it?
Well, not because it isn't implementable.
Sorry, I can't parse that. Should I remove it or not? Is it implementable?
It's my English to be quite unparseable ;) Yes, I showed a possible implementation in the last post: // Macro for when you need a constant expression. // Note that 'expr' appears three times but is // evaluated (at most) once. #define BOOST_IMPLICIT_CAST(dest_type, expr) \ (static_cast< dest_type >( \ sizeof(boost::implicit_cast< dest_type >(expr)) ? \ (expr) : (expr) \ ) \ ) As you see, it's a simple static_cast < type-id > ( expression ) plus the implicit_cast<> test inside a sizeof. The difference with the other version (which had the typename problem you point out) is that sizeof is used inside the *expression* instead of the type-id; thus no template class is used as a helper and no typename keyword must be "hard-coded" inside the macro definition. So: if the only reason why you wanted to remove it was the typename problem then the macro can remain :) OTOH, I understand you might want to remove it anyway, as "implicit casts" in constant expressions aren't the most frequent thing C++ programmers do. It's up to you, I think. I wrote it mainly for fun.
Maybe your code below answers those questions...
Yes, there was a new implementation and a few examples usages.
[...]
I like it. Maybe it's time to document this stuff, too ? ;-)
Ok :) Let me know if you want to leave it or not. Genny.

Gennaro Prota <gennaro_prota@yahoo.com> writes:
On Sat, 22 May 2004 13:11:39 -0400, David Abrahams <dave@boost-consulting.com> wrote:
Maybe your code below answers those questions...
Yes, there was a new implementation and a few examples usages.
[...]
I like it. Maybe it's time to document this stuff, too ? ;-)
Ok :) Let me know if you want to leave it or not.
I don't want to leave it; I want to replace it with your new implementation and add documentation and tests. Actually, I have no time at the moment, so I'd like you to do that, or as much of it as possible. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (2)
-
David Abrahams
-
Gennaro Prota