 |
|
 |
Hi there, correct me if I'm wrong: my understanding of dynamic typing goes like this:
using the sample code in your article, I would like to do the following:
any a = 42; cout << a << endl;
a = 13;
cout << a << endl;
a = "hello";
cout << a << endl;
a = std::string("1234567890");
cout << a.cast<std::string>().c_str() << endl;
int n = 42;
a = &n;
cout << *a.cast<int*>() << endl;
any b = true;
cout << b.cast<bool>() << endl;
swap(a, b);
cout << a.cast<bool>() << endl;
a.cast<bool>() = false;
cout << a.cast<bool>() << endl;
return 0;
What I'm saying is that the class as is can do conversion if there is an assignment operator, but in general, if the context of using the "any" variable is ambiguous, the compiler does not really know how to convert the type. In your example, because "<<" operator for cout is overloaded, then, the explicit call to cast() is necessary to tell the compiler which overload to use. However, this is not what I think of dynamic typing. I don't have to do explicit casts to tell the compiler the actual type of "any".
Is there any way that I can modify the class so that I don't have to do explicit casts?
|
|
|
|
 |
|
 |
I agree. It is very clever made,but the syntax is very ugly.
Also, I tried to use in a real project as a generic datasource, but since
it has no info on the actual type it holds, using it as a return type was not
very nice.
Also, it did not handle simple stuff like
any v1=12;
int num=v1;
I though it would be like the Variant type in VB or Delphi, but perhaps this
is not the goal and speed is.
|
|
|
|
 |
|
 |
we just evaluated this as an alternative to getting "boost" out of our public api ...
on first try/view it looked good ... but the code seems to contain some glitches (especially in copy-c'tor and assignment) ... I'll give some code examples for the found issues, so if somebody want to spent some time on it could fix it ...
Memory Leak example (only with "values" which contain allocated data):
<pre>
cdiggins::any foobar;
while(true)
{
foobar = std::string("12345678901234567890");
}
</pre>
... the assign (with same table) is missing a d'tor on the internal std::string, so the internal memory of std::string wont be freed
... hint: with VC2008 this only happens with strings bigger then 16char (because this std::string impl. contains a special "hack" for short strings)
Crash example (via multiple deletes on the same alloc):
struct foo_s { void* a; void* b; };
cdiggins::any foo = foo_s();
cdiggins::any bar = foo;
foo = bar;
... will crash at end of "block", when foo and bar get freed
|
|
|
|
 |
|
 |
Thank you for finding this problem. I rewrote the whole thing.
The new version is simpler, and I believe fixes the problems you found. Basically only a handful types now support the "small type" policy.
|
|
|
|
 |
|
 |
I think, with this design, you won't be able to find out whether something is stored in cdiggins::any or not. So storing zero or leaving it empty has the same sense. Am i right?
Gokulakannan K.S.
|
|
|
|
 |
|
 |
Right now allocation doesn't take place for types
whose size is <= sizeof( void *).
I think it would be useful if the function table structure
also stored the size of the type.
So for dynamically allocated objects of different classes
but that have the same object size, we can reuse the
memory after calling the destructor for existing object.
any_detail::fxn_ptr_table* x_table = any_detail::get_table::get();
if (table == x_table) {
becomes something like:
any_detail::fxn_ptr_table* x_table = any_detail::get_table::get();
if (table->size_ == x_table->size_ ) {
destruct existing object;
construct new object in place, on existing memory
Anand.
|
|
|
|
 |
|
 |
#include
struct Small {
~Small() { printf("small destructed\n"); }
};
int main()
{
cdiggins::any a;
a = Small();
a = Small();
}
The 2nd assignment fails to call the destructor of the Small object.
So output is
small destructed
small destructed
small destructed
There's another post that mentions this.
Anand.
|
|
|
|
 |
|
 |
Anand Krishnamoorthi wrote: The 2nd assignment fails to call the destructor of the Small object.
So output is
small destructed
small destructed
small destructed
From what I'm seeing, it's the expected output.
int main()
{
cdiggins::any a; // No Small created here
a = Small(); // A Small is created here on the stack, a reference is taken on it, and another Small is allocated in the any, temporary object is destructed -> First line
a = Small(); // A new Small is created, the any's inner object is overwritten with it (unless you use the last version on the forum), the temporary is destructed (second line)
// Small destructor is called here when any is destructed (third line)
}
I'm sure you are refering to the leak the some other people are talking about, but it's corrected since a long time now.
|
|
|
|
 |
|
 |
Hi,
I'm trying to use the example in the article with VC6 and get the following compile error. I'm using latest version from pablo_mag.
any a = 42;
int n = a.cast<int>(); int m = any_cast<int>(a);
I'm a Boost fan, but in this particular case I'd prefer not to have to use it.
Any suggestions?
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
|
|
|
|
 |
|
 |
Unfortunately, VC6 doesn't support that syntax for calling member templates.
The closest one can get is something like the following:
any a = 42;
int n = a.cast_(any::to<int>());
int m = any_cast<int>(a);
But that's obviously not as nice looking as cast() would be.
Do you think it worth adding this?
Pablo Aguilar
pablo dot aguilar at gmail dot com
|
|
|
|
 |
|
 |
Hi Pablo,
Thanks for the prompt response. It isn't the easiest to read solution, but if it works with VC6 without needing boost, then you've sold me.
--edit--
Would it be possible to reproduce the boost::any_cast code in cdiggins::any?
That has a cleaner and simpler syntax?
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-- modified at 20:39 Monday 17th July, 2006
|
|
|
|
 |
|
 |
Hi,
Update's posted.
Changes:
* Added #pragma once support, for whatever it's worth
* Added cast_(to<type>()) syntax for VC6 (well, and every other compiler, of course)
* Added doxygen documentation. Not that it's a must read, just figured I'd start with it, and expand on it later.
It's available here:
www.geocities.com/pablo_aguilarg/any.zip
Chris (Diggins), you might like to update the CodeProject version when you have a chance.
Cheers,
Pablo Aguilar
pablo dot aguilar at gmail dot com
-- modified at 21:56 Monday 17th July, 2006
|
|
|
|
 |
|
 |
Pablo,
Thanks for such a speedy response.
For some reason I thought any_cast was from boost, which was also a problem for me. I've since realised that isn't the case at all. I put it down to spending way too many hours staring at computer screens.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
|
|
|
|
 |
|
 |
Hi,
I'm really excited to use this class but I'm having a compile error with the following code:
const char *message = "hello world";
any a(message);
The errror is:
error C2440: 'reinterpret_cast' : cannot convert from 'void ** ' to 'const char ** '
I was previously using boost::any and I never had this problem. Is there a workaround to this, or am I doing something wrong.
Thanks
|
|
|
|
 |
|
 |
Sorry about replying so late... hadn't checked in for a while.
The code compiles correctly with VC6 and the latest posted version. Could you please e-mail me a full project where this happens? And, what compiler are you using?
Pablo Aguilar
pablo dot aguilar at gmail dot com
|
|
|
|
 |
|
 |
There's an update posted at www.geocities.com/pablo_aguilarg/any.zip
It's likely to be the finished version. There's still tests to write, but I believe the main code to be stable now.
As it stands, it's been tested on VC6.5, BCB6 and VC7.1
|
|
|
|
 |
|
 |
Congratulations! Thank you very much! Is there still a possibility you will take over this article? I think there is a lot of interesting things you could say about your implementation which would be appreciated by the readers.
Christopher Diggins
|
|
|
|
 |
|
 |
The program in my previous thread works without modification in BCB4 using your changes. Interesting.
Again, thanks for making this great tool available.
David
|
|
|
|
 |
|
|
 |
|
 |
Does new updated version need to install boost library?
|
|
|
|
 |
|
 |
Yes it does, it needs the alignment facilities provided by boost.
Pablo
|
|
|
|
 |
|
 |
Is it possible to implement new version but without installation of Boost Library? Just like the original version.
|
|
|
|
 |
|
 |
Well, I guess...
The reason it's using boost is because the original version had slight portability problems. The kind which are highly unlikely to affect you, yet sticking to what the standard says, it's possible they will.
I'll post a "boost free" version later today.
Pablo
|
|
|
|
 |
|
 |
Where is location for download of "Boost Free" version?
|
|
|
|
 |
|
 |
Done.
Same url: http://www.geocities.com/pablo_aguilarg/any.zip
Just define the ANY_DONT_USE_BOOST macro before including any.hpp
Let me know if this works out well for you.
Pablo
|
|
|
|
 |