templates

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 19 12:20:51 PDT 2010


On Mon, 19 Apr 2010 15:16:46 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Mon, 19 Apr 2010 14:16:03 -0400, Ellery Newcomer  
> <ellery-newcomer at utulsa.edu> wrote:
>
>> Hello.
>>
>> Say I have a [struct] template T, which takes a param S.
>>
>> Any T!(S) satisfies a certain template constraint W, so I can use any  
>> T!(S) the same way. I want to be able to store heterogeneous T!(S) in a  
>> single list. Is there any good way to express the type for this?
>
> What you are looking for is a conversion from compile-time interface to  
> runtime interface.  The only drawback is, you can't go backwards (from a  
> runtime interface to a compile-time).
>
> This can be possible in runtime reflection systems, but the theory is  
> that RTTI can be built from compile-time type info.
>
> Here is a quick-and-dirty solution, if you don't mind using  
> classes/interfaces.  You are going to need some sort of runtime  
> interface in order to get this to work, classes/interfaces are not the  
> leanest way to do this, but it should get the job done:
>
> The S is an extra complication that can be factored out, so let's forget  
> about S for now.  Let's assume W defines a single function int  
> foo(int).  Let's make a W interface:
>
> interface IW
> {
>     int foo(int);
> }
>
> Now, we can define a class template to hold your values:
>
> class WByVal(T) if (implementsW!T)

Whoops!  Forgot the interface!

class WByVal(T) : IW if (implementsW!T)

-Steve


More information about the Digitalmars-d-learn mailing list