Re: [Boost-users] boost any questions and praise
[I fear this conversation is drifting off-topic, but I hope the moderators will forgive a further reply]
and because it is an any he has no means of knowing for sure if the contents are streamable at instantiation time.
Don't put an object into the any that can't be cout and then try and cout the any.
He has to "trust to luck".
His code put the object into the any in the first place. No luck involved. Or it's someone elses code but he knows what someone elses code is giving. Ah you are lucky. You live in a world where you and your co-developers never make any mistakes and the documentation is always up-to-date. I am more fallible and make mistakes and if possible I'd prefer to be told about them via a compile-time error rather than blowing the stack at runtime. There is more to an object than it's type. You can't just guess what to give a function based on only the information that it wants one
unsigned integer. Sure - I don't think anyone has said type-safety is a panacea for all coding ills or a replacement for documentation. But the consensus is that it very helpful in C++. Incidentally, you may be interested in the Constrained Value library (http://student.agh.edu.pl/~kawulak/constrained_value/index.html). [1] So if your function needs something in [a,b] rather than [0,UINT_MAX] the type system can give some assistance. [snip irrelevant stuff about "lockup safe" - it's a non-sequitur ] I do appreciate there are different views here, however if one has
chosen boost::any already then they already know what they want, especially given the stuff in std. Suggesting that because you can
cause an error an error could unexpectedly occur when you pass unknown objects into an unknown method in a non sensible example that wouldn't happen isn't helpful. You have to appreciate where we are coming from. Your original post had code where you tried to pass the result of a runtime method as a template parameter. This suggests you are not hugely familiar with C++ and perhaps Boost.Any, so people naturally ponder whether Boost.Any really is the right choice (often it isn't) and try to help by making sure the OP has heard about the alternatives. The documentation says that for boost::any a program doesn't crash
if you try and cast an any into a type it was not set as, it gives a null instead. Actually, If try to retrieve a pointer to the type, it would return a null, if you tried to retrieve a value, it would throw bad_any_cast. For crashing on stream insertion when it's contents can't be inserted vs giving an error that will stop the program more safely
and tell the coder what went wrong, I think the latter is more desirable, unless there is a significant performance difference in
which case either one is valid. Right, if you went down a has_operator_stream route, you could throw an exception instead of blowing the stack. Feel free to offer a patch along those lines if you want , but it opens up a can worms. If operator
Links: ------ [1] http://student.agh.edu.pl/~kawulak/constrained_value/index.html).
Incidentally, you may be interested in the Constrained Value library ( http://student.agh.edu.pl/~kawulak/constrained_value/index.htmlhttp://student.agh.edu.pl/%7Ekawulak/constrained_value/index.html )
I like that idea. I quite like the idea of things like an 8bit number that stores values between 0 and 1. I like the idea of a number type, that can store numbers in all sorts of forms, an example could be a "8bit integer / 8bit integer" or "4bit float" or whatever, you could instruct it with methods to do things such as. Have a scale defining it's limits and possibly also set limits, store 2 numbers in 3 bits. Store a value in a number of forms so that it is always quick to access for different uses or have it store it in the most specially efficient form for the value, have a number that expanded until a certain number of decimal places was reached, or forever, or all sorts of other related things. I have some idea about a possible way to represent the expandable number in binary as well. At least in C++ I would expect this to have such a performance overhead as to make it not worth it even trying to implement though, I guess I don't much like lexical casting automatically or using the cast syntax, prefer .toString() type methods, but especially between numerical types where it seems like it's always about fundamentally the same thing, a number.
I do appreciate there are different views here, however if one has
chosen boost::any already then they already know what they want, especially given the stuff in std. Suggesting that because you can cause an error an error could unexpectedly occur when you pass unknown objects into an unknown method in a non sensible example that wouldn't happen isn't helpful.
You have to appreciate where we are coming from. Your original post had code where you tried to pass the result of a runtime method as a template parameter. This suggests you are not hugely familiar with C++ and perhaps Boost.Any, so people naturally ponder whether Boost.Any really is the right choice (often it isn't) and try to help by making sure the OP has heard about the alternatives.
Your right, I am fairly new to C++.
The documentation says that for boost::any a program doesn't crash
if you try and cast an any into a type it was not set as, it gives a null instead.
Actually, If try to retrieve a pointer to the type, it would return a null, if you tried to retrieve a value, it would throw bad_any_cast.
Thanks.
For crashing on stream insertion when it's contents can't be
inserted vs giving an error that will stop the program more safely and tell the coder what went wrong, I think the latter is more desirable, unless there is a significant performance difference in which case either one is valid.
Right, if you went down a has_operator_stream route, you could throw an exception instead of blowing the stack.
Feel free to offer a patch along those lines if you want, but it opens up a can worms. If operator<<, why not operator+, or member foo() etc... I am sceptical that the maintainer would accept it.
Does adobe::polly offer a way to call methods on the anys content without unwrapping it? Could this be done generically for any method (I know there would be type/method safety issues)? I'm using Peters version. My stuff is ambitious but higher level, I can't write things like this. It would possibly make a good case to have two versions of boost::any if the generic methods could be implemented, would also close the can of worms, nothing would ever need to be added to it again. Maybe I have overlooked that a stream and other such things are probably not proper methods at any level and so there are a few worms, but a finite number.
2010/2/3 Alan Tennant
Does adobe::polly offer a way to call methods on the anys content without unwrapping it? Could this be done generically for any method (I know there would be type/method safety issues)?
Yes, you can do that with adobe poly and it's perfectly safe. Take a look at the examplehttp://stlab.adobe.com:8080/@rev1=head@//adobe_source_libraries/test/poly/po... which adds method size() to poly without modifying poly's source code. You could do the same to add operator<<. Roman Perepelitsa.
I get an error that points me to this page
http://stlab.adobe.com/wiki/index.php/Troubleshooting
Probably because I'm using VC++9 not 8.
Just to clarify I meant this.
std::string s1 = "1234";
std::cout<
2010/2/3 Alan Tennant
Does adobe::polly offer a way to call methods on the anys content without unwrapping it? Could this be done generically for any method (I know there would be type/method safety issues)?
Yes, you can do that with adobe poly and it's perfectly safe.
Take a look at the examplehttp://stlab.adobe.com:8080/@rev1=head@//adobe_source_libraries/test/poly/po... which adds method size() to poly without modifying poly's source code. You could do the same to add operator<<.
Roman Perepelitsa.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
2010/2/3 Alan Tennant
I get an error that points me to this page http://stlab.adobe.com/wiki/index.php/Troubleshooting Probably because I'm using VC++9 not 8.
Just to clarify I meant this.
std::string s1 = "1234"; std::cout<
adobe::polly p1; p1 = s1; std::cout<
std::string s2; s2 = polly_caststd::string(p1); std::cout<
The syntax is more like this: poly<ostreamable> p; p = 42; cout << p; // writes "42" p = "hello"; cout << p; // writes "hello"; p = vector<int>(); // does not compile, because vector<int> is not ostreamable. Note that you should implement ostreamable.
So it's not going to be safe as in "type safe" but it may well be perfectly safe.
I don't understand this. Roman Perepelitsa.
The syntax is more like this:
poly<ostreamable> p; p = 42; cout << p; // writes "42" p = "hello"; cout << p; // writes "hello"; p = vector<int>(); // does not compile, because vector<int> is not ostreamable.
Note that you should implement ostreamable.
Thank you for the explanation. So it's not going to be safe as in "type safe" but it may well be perfectly
safe.
I don't understand this.
As I described which is not quite how it works wouldn't be type safe as you could put something without a method in then call that method but it would be safe as like everything else in coding in it wouldn't break unless you did something wrong. polly<allMethodsAble> p1; This would be useful, but if it won't do this then it's any_stream. Contact the maintainer to find out if he wants to put the already implemented stream support or <...able> into any.
Why is the member "content" private in any? I tried to make it public but I
actually couldn't find it in the .h file.
On 4 February 2010 10:36, Alan Tennant
The syntax is more like this:
poly<ostreamable> p; p = 42; cout << p; // writes "42" p = "hello"; cout << p; // writes "hello"; p = vector<int>(); // does not compile, because vector<int> is not ostreamable.
Note that you should implement ostreamable.
Thank you for the explanation.
So it's not going to be safe as in "type safe" but it may well be
perfectly safe.
I don't understand this.
As I described which is not quite how it works wouldn't be type safe as you could put something without a method in then call that method but it would be safe as like everything else in coding in it wouldn't break unless you did something wrong.
polly<allMethodsAble> p1;
This would be useful, but if it won't do this then it's any_stream.
Contact the maintainer to find out if he wants to put the already implemented stream support or <...able> into any.
participants (3)
-
Alan Tennant
-
pete@pcbartlett.com
-
Roman Perepelitsa