
Hi, I've checked the 'any' library, and I have a suggestion. Wouldn't it be nicer if class any will have a member method like: template <typename T> T get() const; or maybe even: template <typename T> const T& get() const; template <typename T> T& get(); In my humble opinion, that would look better of the current 'any_cast' way of doing it. Thanks, Yuval Ronen

From: "Yuval Ronen" <ronen_yuval@yahoo.com>
Wouldn't it be nicer if class any will have a member method like:
template <typename T> T get() const;
or maybe even:
template <typename T> const T& get() const; template <typename T> T& get();
So you'd prefer writing this: boost::any<...> a(...); int i(a.template get<int>()); to this? boost::any<...> a(...); int i(any_cast<int>(a)); The latter has the advantage of looking just like static_cast et al, avoiding the ugly template qualification of the member function invocation, and being shorter. I think the current approach is better. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Wouldn't it be nicer if class any will have a member method like:
template <typename T> T get() const;
or maybe even:
template <typename T> const T& get() const; template <typename T> T& get();
So you'd prefer writing this:
boost::any<...> a(...); int i(a.template get<int>());
to this?
boost::any<...> a(...); int i(any_cast<int>(a));
The latter has the advantage of looking just like static_cast et al, avoiding the ugly template qualification of the member function invocation, and being shorter.
I think the current approach is better.
I blieve this a matter of point of view. I think of it as "extracting the inner object" within the any object, so get() sounds more intuitive. I'd much rather writing something like: int a = some_any.get<int>(); You think of it as a cast so any_cast sounds better to you. My guess that neither of us will be able to convince the other, so I'll suggest some sort of compromise: why don't we have both? The get() method(s) will be used by weird people like me who want to "extract the value/reference of the inner object", while the any_cast will be used by all the rest. If you'll implement the any_cast using the new get() method(s), you'll get some more benefits, such as removing the 'friend any_cast' and possibly removing the const_cast from one of the any_cast<>s. It's just my opinion, Yuval

From: "Yuval Ronen" <ronen_yuval@yahoo.com>
Wouldn't it be nicer if class any will have a member method like:
template <typename T> T get() const;
or maybe even:
template <typename T> const T& get() const; template <typename T> T& get();
So you'd prefer writing this:
boost::any<...> a(...); int i(a.template get<int>());
to this?
boost::any<...> a(...); int i(any_cast<int>(a));
The latter has the advantage of looking just like static_cast et al, avoiding the ugly template qualification of the member function invocation, and being shorter.
I blieve this a matter of point of view. I think of it as "extracting the inner object" within the any object, so get() sounds more intuitive. I'd much rather writing something like: int a = some_any.get<int>();
But you can't. You have to write the following, as I showed above: int a = some_any.template get<int>();
You think of it as a cast so any_cast sounds better to you. My guess that neither of us will be able to convince the other, so I'll suggest some sort of compromise: why don't we have both?
Do you still like your version? Yes, I think cast fits quite nicely, but I hate forcing the use of the "template" qualifier in function calls when I can avoid it. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart wrote:
From: "Yuval Ronen" <ronen_yuval@yahoo.com>
function invocation, and being shorter.
I blieve this a matter of point of view. I think of it as "extracting the inner object" within the any object, so get() sounds more intuitive. I'd much rather writing something like: int a = some_any.get<int>();
But you can't. You have to write the following, as I showed above:
int a = some_any.template get<int>();
You think of it as a cast so any_cast sounds better to you. My guess that neither of us will be able to convince the other, so I'll suggest some sort of compromise: why don't we have both?
Do you still like your version?
Yes, I think cast fits quite nicely, but I hate forcing the use of the "template" qualifier in function calls when I can avoid it.
I can just imagine the deluge of support emails asking why some_any.get<int>() won't work! Jeff

Rob Stewart wrote:
From: "Yuval Ronen" <ronen_yuval@yahoo.com>
But you can't. You have to write the following, as I showed above:
int a = some_any.template get<int>();
?? There are no dependent types in that utterance. Unless some_any is not known to be a boost::any (?) Am I missing something? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

From: David Abrahams <dave@boost-consulting.com>
Rob Stewart wrote:
From: "Yuval Ronen" <ronen_yuval@yahoo.com>
But you can't. You have to write the following, as I showed above:
int a = some_any.template get<int>();
?? There are no dependent types in that utterance. Unless some_any is not known to be a boost::any (?) Am I missing something?
I tried to track down the relevant section in the standard to confirm what I wrote, but I couldn't. I was relying on C++PL, 3rd Ed, App C, section C.13.6, "Template as a Qualifier." The example is (reduced): struct Memory { template <typename T> T * get_new(); }; template <typename Allocator> void f(Allocator & m) { int * p1 = m.get_new<int>(); // error int * p2 = m.template get_new<int>(); } This Memory::get_new() is used within a function template, but Stroustrup doesn't mention that it is precisely because get_new() is dependent that "template" is needed, so I didn't think that it was a dependent type. Thinking further, I realized that m.get_new<int>() actually means m.Allocator::get_new<int>(), so I now see that it is a dependent type according to 14.6.2.1/1. In the OP's case, the invoked member function would be any<T>'s, not T's, so it isn't dependent and "template" isn't needed, which is what you implied. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

I have to admit two things in shame: 1. My knowledge of the C++ standard is not that profound as to decide whether the "template" is necessary or not. 2. I also didn't quite understand your reply. What was the conclusion? Is the "template" needed to exist? Not needed to exist? Needed not to exist? So, in light of these shortcomings of mine, I decied to give it a try. I testes both versions (with and without the "template") on 3 compilers: GNU GCC 3.2, GNU GCC 3.4.2 and MSVC 7.1. The results were that GCC 3.2 and MSVC 7.1 accepted both versions, while GCC 3.4.2 accepted only the version without the "template". So what's the conlusion now? Are these suggested get() methods still such a bad idea? Will they ever see the daylight?
But you can't. You have to write the following, as I showed above:
int a = some_any.template get<int>();
?? There are no dependent types in that utterance. Unless some_any is not known to be a boost::any (?) Am I missing something?
I tried to track down the relevant section in the standard to confirm what I wrote, but I couldn't. I was relying on C++PL, 3rd Ed, App C, section C.13.6, "Template as a Qualifier."
The example is (reduced):
struct Memory { template <typename T> T * get_new(); };
template <typename Allocator> void f(Allocator & m) { int * p1 = m.get_new<int>(); // error int * p2 = m.template get_new<int>(); }
This Memory::get_new() is used within a function template, but Stroustrup doesn't mention that it is precisely because get_new() is dependent that "template" is needed, so I didn't think that it was a dependent type.
Thinking further, I realized that m.get_new<int>() actually means m.Allocator::get_new<int>(), so I now see that it is a dependent type according to 14.6.2.1/1.
In the OP's case, the invoked member function would be any<T>'s, not T's, so it isn't dependent and "template" isn't needed, which is what you implied.
-- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer; _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Yuval Ronen wrote:
I have to admit two things in shame: 1. My knowledge of the C++ standard is not that profound as to decide whether the "template" is necessary or not. 2. I also didn't quite understand your reply. What was the conclusion? Is the "template" needed to exist? Not needed to exist? Needed not to exist?
So, in light of these shortcomings of mine, I decied to give it a try. I testes both versions (with and without the "template") on 3 compilers: GNU GCC 3.2, GNU GCC 3.4.2 and MSVC 7.1. The results were that GCC 3.2 and MSVC 7.1 accepted both versions, while GCC 3.4.2 accepted only the version without the "template".
So what's the conlusion now?
They are not needed when /using/ any objects. Have a look at section 5.1 of C++ Templates, The Complete Guide, Vandevoorde & Josuttis for a quick overview on the topic when to use the keywords "typename" and "template" to disambiguate template code. Short rule: never do this outside of template code, and only for constructs depending on the template parameters. greetings, Roland
Are these suggested get() methods still such a bad idea? Will they ever see the daylight?
But you can't. You have to write the following, as I showed above:
int a = some_any.template get<int>();
?? There are no dependent types in that utterance. Unless some_any is not known to be a boost::any (?) Am I missing something?
I tried to track down the relevant section in the standard to confirm what I wrote, but I couldn't. I was relying on C++PL, 3rd Ed, App C, section C.13.6, "Template as a Qualifier."
The example is (reduced):
struct Memory { template <typename T> T * get_new(); };
template <typename Allocator> void f(Allocator & m) { int * p1 = m.get_new<int>(); // error int * p2 = m.template get_new<int>(); }
This Memory::get_new() is used within a function template, but Stroustrup doesn't mention that it is precisely because get_new() is dependent that "template" is needed, so I didn't think that it was a dependent type.
Thinking further, I realized that m.get_new<int>() actually means m.Allocator::get_new<int>(), so I now see that it is a dependent type according to 14.6.2.1/1.
In the OP's case, the invoked member function would be any<T>'s, not T's, so it isn't dependent and "template" isn't needed, which is what you implied.
-- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer; _______________________________________________ Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Yuval Ronen <ronen_yuval <at> yahoo.com> writes:
Hi, I've checked the 'any' library, and I have a suggestion. Wouldn't it be nicer if class any will have a member method like:
template <typename T> T get() const;
or maybe even:
template <typename T> const T& get() const; template <typename T> T& get();
I proposed enhancing any_cast by adding support for extracting reference to the held value some time ago(and it was proposed in the past), but nobody replied to that message. Looks like Boost.Any maintainer's on the vacation. Basic idea was to make this work (now you have to use some obscure syntax): T& object = any_cast<T&>(any_object);

Mikhail Glushenkov wrote:
I proposed enhancing any_cast by adding support for extracting reference to the held value some time ago(and it was proposed in the past),
That was probably me.
but nobody replied to that message. Looks like Boost.Any maintainer's on the vacation. Basic idea was to make this work (now you have to use some obscure syntax): T& object = any_cast<T&>(any_object);
I would like to see exactly the same semantics as for boost::get in the variant library: http://tinyurl.com/43nks Stefan

Stefan Slapeta wrote:
Mikhail Glushenkov wrote:
I proposed enhancing any_cast by adding support for extracting reference to the held value some time ago(and it was proposed in the past),
That was probably me.
but nobody replied to that message. Looks like Boost.Any maintainer's on the vacation. Basic idea was to make this work (now you have to use some obscure syntax): T& object = any_cast<T&>(any_object);
I would like to see exactly the same semantics as for boost::get in the variant library: http://tinyurl.com/43nks
Ouch; I didn't know variant was defining get. This seems like a ripe area for conflicts with Boost.Graph, which also has many free get() functions for its property maps. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

On Dec 8, 2004, at 3:25 PM, Mikhail Glushenkov wrote:
Yuval Ronen <ronen_yuval <at> yahoo.com> writes:
Hi, I've checked the 'any' library, and I have a suggestion. Wouldn't it be nicer if class any will have a member method like:
template <typename T> T get() const;
I personally don't see any benefit to this. Maybe it looks a little more "OO", but any_cast truly is a cast (as with
I proposed enhancing any_cast by adding support for extracting reference to the held value some time ago(and it was proposed in the past), but nobody replied to that message.
I thought we had decided that we wanted to do it (a very long time ago), but nobody actually supplied a portable implementation. It seems that it wouldn't be too hard to do.
Looks like Boost.Any maintainer's on the vacation.
Kevlin has not been active recently, and for all practice purposes Boost.Any does not have a maintainer. Doug
participants (9)
-
David Abrahams
-
Doug Gregor
-
Jeff Flinn
-
Mikhail Glushenkov
-
Rob Stewart
-
Roland Schwarz
-
Roland Weiss
-
Stefan Slapeta
-
Yuval Ronen