[typeof][implementation]why not use decltype or conditional expr?

Why couldn't the BOOST_TYPEOF macro be defined to simply use the new keyword, decltype, if it's available? I assume the macro, BOOST_HAS_DECLTYPE, indicates whether it is available. And if decltype not available, why couldn't it use the conditional expression method described here: http://www.artima.com/cppsource/foreach2.html I would think that would be simpler and more portable. -regards Larry

AMDG On 08/03/2012 09:22 AM, Larry Evans wrote:
Why couldn't the BOOST_TYPEOF macro be defined to simply use the new keyword, decltype, if it's available? I assume the macro, BOOST_HAS_DECLTYPE, indicates whether it is available.
I assume Boost.Typeof will eventually be updated to use decltype.
And if decltype not available, why couldn't it use the conditional expression method described here:
http://www.artima.com/cppsource/foreach2.html
I would think that would be simpler and more portable.
I don't see how it's possible to implement BOOST_TYPEOF this way. In Christ, Steven Watanabe

On 08/03/12 13:46, Steven Watanabe wrote:
AMDG
On 08/03/2012 09:22 AM, Larry Evans wrote:
Why couldn't the BOOST_TYPEOF macro be defined to simply use the new keyword, decltype, if it's available? I assume the macro, BOOST_HAS_DECLTYPE, indicates whether it is available.
I assume Boost.Typeof will eventually be updated to use decltype.
And if decltype not available, why couldn't it use the conditional expression method described here:
http://www.artima.com/cppsource/foreach2.html
I would think that would be simpler and more portable.
I don't see how it's possible to implement BOOST_TYPEOF this way.
On page: http://www.artima.com/cppsource/foreach2.html there's this: If only there were a way to get the type of an expression without evaluating the expression. There is! The unique properties of the conditional operator allow us to sidestep the issue entirely. The ENCODED_TYPEOF macro defined below encodes the type of an expression without evaluating it. Read on to see how it works. IIUC, the purpose of decltype is to "get the type of an expression without evaluating the expression"; hence, if the current BOOST_TYPEOF can be implemented with decltype, and the above quote from foreach2.html is correct, then the current BOOST_TYPEOF can be implemented with the method described in the foreach2.html page. Am I missing something? -regards, Larry

Why couldn't the BOOST_TYPEOF macro be defined to simply use the new keyword, decltype, if it's available? I assume the macro, BOOST_HAS_DECLTYPE, indicates whether it is available.
I assume Boost.Typeof will eventually be updated to use decltype.
And if decltype not available, why couldn't it use the conditional expression method described here:
http://www.artima.com/cppsource/foreach2.html
I would think that would be simpler and more portable.
I don't see how it's possible to implement BOOST_TYPEOF this way.
On page:
http://www.artima.com/cppsource/foreach2.html
there's this:
If only there were a way to get the type of an expression without evaluating the expression. There is! The unique properties of the conditional operator allow us to sidestep the issue entirely. The ENCODED_TYPEOF macro defined below encodes the type of an expression without evaluating it. Read on to see how it works.
IIUC, the purpose of decltype is to "get the type of an expression without evaluating the expression"; hence, if the current BOOST_TYPEOF can be implemented with decltype, and the above quote from foreach2.html is correct, then the current BOOST_TYPEOF can be implemented with the method described in the foreach2.html page.
Am I missing something?
The conditional operator trick gives you an *expression* E of the same typeas another expression F, without evaluating F. Typeof/decltype gives you the *type* of an expression F. You can't go from the first to the second without... well typeof/decltype. Regards,Nate

On 08/03/12 18:07, Nathan Ridge wrote: [snip]
And if decltype not available, why couldn't it use the conditional expression method described here:
http://www.artima.com/cppsource/foreach2.html
I would think that would be simpler and more portable.
I don't see how it's possible to implement BOOST_TYPEOF this way.
On page:
http://www.artima.com/cppsource/foreach2.html
there's this:
If only there were a way to get the type of an expression without evaluating the expression. There is! The unique properties of the conditional operator allow us to sidestep the issue entirely. The ENCODED_TYPEOF macro defined below encodes the type of an expression without evaluating it. Read on to see how it works.
IIUC, the purpose of decltype is to "get the type of an expression without evaluating the expression"; hence, if the current BOOST_TYPEOF can be implemented with decltype, and the above quote from foreach2.html is correct, then the current BOOST_TYPEOF can be implemented with the method described in the foreach2.html page.
Am I missing something?
The conditional operator trick gives you an *expression* E of the same typeas another expression F, without evaluating F. Typeof/decltype gives you the *type* of an expression F. You can't go from the first to the second without... well typeof/decltype.
Regards,Nate
Thanks Nate. The emphasis on *expression* and *type* cleared things up. The foreach2.html phrase: get the type of an expression was kinda easy to misinterpret. Only after careful examination of the code did the actual purpose, the one you gave, become clear. -regards, Larry

Why couldn't the BOOST_TYPEOF macro be defined to simply use the new keyword, decltype, if it's available? I assume the macro, BOOST_HAS_DECLTYPE, indicates whether it is available.
And if decltype not available, why couldn't it use the conditional expression method described here:
Actually, the rvalue detection techniques in the article could be used to create a better decltype emulation on older compilers. I use this here: https://github.com/pfultz2/Zelda/blob/master/zelda/typeof.h Its a macro called ZELDA_XTYPEOF, which in C++11 is the equivalent of decltype(()) with double parenthesis. On older compilers, it will bind to a reference, unless its an rvalue which it will not have a reference. For example, say you had these functions: int by_value(); int& by_ref(); const int& by_const_ref(); Using typeof will only give int or const int, but with decltype and ZELDA_XTYPEOF it will accurately give you the return type: ZELDA_XTYPEOF(by_value()) // is int ZELDA_XTYPEOF(by_ref()) // is int & ZELDA_XTYPEOF(by_const_ref()) // is const int & Paul

paul Fultz wrote:
And if decltype not available, why couldn't it use the conditional expression method described here:
Actually, the rvalue detection techniques in the article could be used to create a better decltype emulation on older compilers. I use this here:
https://github.com/pfultz2/Zelda/blob/master/zelda/typeof.h
Its a macro called ZELDA_XTYPEOF, which in C++11 is the equivalent of decltype(()) with double parenthesis. On older compilers, it will bind to a reference, unless its an rvalue which it will not have a reference.
On C++03-compliant compilers, you cannot use Boost.Foreach's technique to detect rvalueness for classes with a template conversion operator. Regards, Michel
participants (5)
-
Larry Evans
-
Michel Morin
-
Nathan Ridge
-
paul Fultz
-
Steven Watanabe