Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++Goto page 1, 2, 3, 4  Next

Who discovered the type erasure technique in C++?

 
Jump to:  
 
Roshan Naik
PostPosted: Sun Aug 17, 2008 2:48 pm    Post subject: Who discovered the type erasure technique in C++?
       
I am wondering who first figured out how to do this in C++ ?
Boost::Function and Boost::Expressive libraries both use it and my Castor
library (mpprogramming.com/cpp) also relies on it very heavily. I thought
Eric Niebler might be the one, but at NWCPP he told me that he too had
picked it up from somewhere else.
IMHO, this is one of the most significant discoveries done in the library
space ... with a lot unrealized potential.
-Roshan

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Mon Aug 18, 2008 8:55 pm    Post subject: Re: Who discovered the type erasure technique in C++?
       
On 17 août, 16:48, "Roshan Naik" <naikr...@gmail.com> wrote:
Quote:
I am wondering who first figured out how to do this in C++ ?

Isn't it simply dynamic dispatch with value semantics?


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Maxim Yegorushkin
PostPosted: Mon Aug 18, 2008 8:59 pm    Post subject: Re: Who discovered the type erasure technique in C++?
       
On Aug 17, 3:48 pm, "Roshan Naik" <naikr...@gmail.com> wrote:
Quote:
I am wondering who first figured out how to do this in C++ ?
Boost::Function and Boost::Expressive libraries both use it and my Castor
library (mpprogramming.com/cpp) also relies on it very heavily. I thought
Eric Niebler might be the one, but at NWCPP he told me that he too had
picked it up from somewhere else.

I saw this idiom first in boost::any which, I guess, one of the oldest
boost libraries.

Max


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Guest
PostPosted: Tue Aug 19, 2008 12:42 am    Post subject: Re: Who discovered the type erasure technique in C++?
       
On 17 Aug, 15:48, "Roshan Naik" <naikr...@gmail.com> wrote:
Quote:
I am wondering who first figured out how to do this in C++ ?
Boost::Function and Boost::Expressive libraries both use it and my Castor
library (mpprogramming.com/cpp) also relies on it very heavily. I thought
Eric Niebler might be the one, but at NWCPP he told me that he too had
picked it up from somewhere else.

I have never heard of type erasure in C++. I have come across it in
java though. I always thought it was there for compatibility with
containers before java had generics. What exactly does it mean in C++
please?

Regards,

Andrew Marlow

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Andrei Alexandrescu
PostPosted: Tue Aug 19, 2008 9:41 am    Post subject: Re: Who discovered the type erasure technique in C++?
       
marlow.andrew@googlemail.com wrote:
Quote:
On 17 Aug, 15:48, "Roshan Naik" <naikr...@gmail.com> wrote:
I am wondering who first figured out how to do this in C++ ?
Boost::Function and Boost::Expressive libraries both use it and my Castor
library (mpprogramming.com/cpp) also relies on it very heavily. I thought
Eric Niebler might be the one, but at NWCPP he told me that he too had
picked it up from somewhere else.

I have never heard of type erasure in C++. I have come across it in
java though. I always thought it was there for compatibility with
containers before java had generics. What exactly does it mean in C++
please?

Regards,

Andrew Marlow

Without being called as such, it's of use in the implementation of
Variant (www.oonumerics.org/tmpw01/alexandrescu.pdf, see also three
subsequent articles LINK
LINK
LINK).

What gives it away in C++ is a type with a template constructor that
uses the type to create a dynamically polymorphic object.


Andrei

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Roshan Naik
PostPosted: Tue Aug 19, 2008 9:58 am    Post subject: Re: Who discovered the type erasure technique in C++?
       
{ Edits: top-posting rearranged; re top-posting see FAQ item 5.4. -mod }

On Mon, 18 Aug 2008 13:55:17 -0700, Mathias Gaunard <loufoque@gmail.com>
wrote:

Quote:
On 17 août, 16:48, "Roshan Naik" <naikr...@gmail.com> wrote:
I am wondering who first figured out how to do this in C++ ?

Isn't it simply dynamic dispatch with value semantics?

yes .. but I consider both those aspects as "side effects" of the
implementation. In other words.. it is not a description of the feature
but a description of how it is made to work.

-Roshan


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Roshan Naik
PostPosted: Tue Aug 19, 2008 9:58 am    Post subject: Re: Who discovered the type erasure technique in C++?
       
{ Edits: top-posting rearranged; re top-posting see FAQ item 5.4. -mod }

On Mon, 18 Aug 2008 17:42:31 -0700, <marlow.andrew@googlemail.com> wrote:
Quote:

I have never heard of type erasure in C++. I have come across it in
java though. I always thought it was there for compatibility with
containers before java had generics. What exactly does it mean in C++
please?

Regards,

Andrew Marlow

Here is how I would describe type erasure in the context of C++ :

Lets say T denotes all types that meet some criteria C. And C is known at
compile time.
This criteria can be something like "has member function foo()". So type
erasure is about
defining a type C where we encode this criteria and achieve the following
goal :


Lets say we have two types MyT and HisT which ,in addition to fulfilling
the criteria C, can potentially have other members.

struct MyT {
void foo();
void sayHi();
};

struct HisT {
void foo();
void bar();
void sayHello();
};

Note that MyT and HisT do not derive from anything and there are no
conversion operators defined to can convert MyT and HisT to C. Also type C
does not know about the existence of MyT and HisT.

But we should be able to :

C erase1 = MyT(); // no casting reqd !
C erase2 = HisT();
C erase3 = std::string("hello"); // Compiler error : string::foo() not
found!

erase1.foo(); // calls MyT::foo()
erase2.foo(); // calls HisT::foo()
erase2.sayHello(); // compiler error


After the values have been assigned to erase1 and erase2 the original type
info is now lost (or erased) and thus we are limited to operating only on
member foo(). All we know for certain after the assignment is that
original types implemented member foo.

This is similar to being able to assign a pointer of a derived class to a
pointer of a base class. But the key difference with type erasure, is that
type C does not need to be authored prior to authoring MyT and HisT. So
MyT and HisT are not tightly coupled to C.

With C++ duck typing, we could perhaps achieve something close but only if
this code that resides inside a templated type or function having C as
type parameter. I apologize if this description is not very easy to
understand.

-Roshan

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Tue Aug 19, 2008 9:17 pm    Post subject: Re: Who discovered the type erasure technique in C++?
       
On 19 août, 11:58, "Roshan Naik" <naikr...@gmail.com> wrote:

Quote:
Lets say T denotes all types that meet some criteria C. And C is known at
compile time.
This criteria can be something like "has member function foo()". So type
erasure is about
defining a type C where we encode this criteria and achieve the following
goal

That's actually dynamic structural subtyping.
That's a special case of type erasure.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Marco Manfredini
PostPosted: Tue Aug 19, 2008 9:18 pm    Post subject: Re: Who discovered the type erasure technique in C++?
       
Roshan Naik wrote:

Quote:
Here is how I would describe type erasure in the context of C++ :

Lets say T denotes all types that meet some criteria C. And C is known
at compile time.
This criteria can be something like "has member function foo()". So
type erasure is about
defining a type C where we encode this criteria and achieve the
following goal :

If defined that way, then "type erasure" is an attempt to copy
Haskell's type classes, introduced 1989 by Wadler & Blott.

Marco


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Larry Evans
PostPosted: Tue Aug 19, 2008 9:24 pm    Post subject: type erasure vs witness (was Re: Who discovered the type era
       
On 08/19/08 04:58, Roshan Naik wrote:
[snip]
Quote:

Here is how I would describe type erasure in the context of C++ :

Lets say T denotes all types that meet some criteria C. And C is known at
compile time.
This criteria can be something like "has member function foo()". So type
erasure is about
defining a type C where we encode this criteria and achieve the following
goal :


Lets say we have two types MyT and HisT which ,in addition to fulfilling
the criteria C, can potentially have other members.

struct MyT {
void foo();
void sayHi();
};

struct HisT {
void foo();
void bar();
void sayHello();
};

Note that MyT and HisT do not derive from anything and there are no
conversion operators defined to can convert MyT and HisT to C. Also type C
does not know about the existence of MyT and HisT.

But we should be able to :

C erase1 = MyT(); // no casting reqd !
C erase2 = HisT();
C erase3 = std::string("hello"); // Compiler error : string::foo() not
found!

erase1.foo(); // calls MyT::foo()
erase2.foo(); // calls HisT::foo()
erase2.sayHello(); // compiler error
[snip]
I apologize if this description is not very easy to
understand.

-Roshan

Roshan,


I've just been reading about Haskell Open Witnesses:

LINK

So far, I only understand part of what a Witness is; however,
it sounds somewhat like your description. for example, at the
top of Section 2 of the .pdf there's:

A witness is a value that witnesses some sort of constraint
on some list of type variables.

The constraint in your example is that the types must contain
a foo() method.

So, my question is, *is* there some similarity between
type erasure in c++ and haskell witnesses? I thought this
would be useful since the .pdf file also mentioned GADT:

LINK

and several months(maybe years) ago someone on this list
wondered why there was no GADT library. It would be nice
if someone these different concepts could be related.

-Larry



--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 4 .:. Goto page 1, 2, 3, 4  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

titan poker materac Porównanie Cen OC wypożyczalnia samochodów kraków Heart music