Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-2008, 03:48 PM
Roshan Naik
 
Posts: n/a
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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-18-2008, 09:55 PM
Mathias Gaunard
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

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?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-18-2008, 09:59 PM
Maxim Yegorushkin
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

On Aug 17, 3:48 pm, "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 saw this idiom first in boost::any which, I guess, one of the oldest
boost libraries.

Max


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-19-2008, 01:42 AM
marlow.andrew@googlemail.com
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

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

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-19-2008, 10:41 AM
Andrei Alexandrescu
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

marlow.andrew@googlemail.com wrote:
> 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 http://www.erdani.org/publications/cuj-04-2002.html,
http://www.erdani.org/publications/cuj-06-2002.html,
http://www.erdani.org/publications/cuj-08-2002.html).

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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 08-19-2008, 10:58 AM
Roshan Naik
 
Posts: n/a
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:

> 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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 08-19-2008, 10:58 AM
Roshan Naik
 
Posts: n/a
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:
>
> 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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 08-19-2008, 10:17 PM
Mathias Gaunard
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

On 19 août, 11:58, "Roshan Naik" <naikr...@gmail.com> wrote:

> 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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #9 (permalink)  
Old 08-19-2008, 10:18 PM
Marco Manfredini
 
Posts: n/a
Re: Who discovered the type erasure technique in C++?

Roshan Naik wrote:

> 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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #10 (permalink)  
Old 08-19-2008, 10:24 PM
Larry Evans
 
Posts: n/a
type erasure vs witness (was Re: Who discovered the type erasure technique in C++?

On 08/19/08 04:58, Roshan Naik wrote:
[snip]
>
> 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:

http://semantic.org/stuff/Open-Witnesses.pdf

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:

http://research.microsoft.com/~akenn...cs/gadtoop.pdf

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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:13 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Cell Phones | Ringtones | Personal Loans | Loans | Current Accounts



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114