Thread overview | |||||
---|---|---|---|---|---|
|
May 17, 2007 can I get public alias to private templates? | ||||
---|---|---|---|---|
| ||||
I have a struct like this Struct { void go(T)(T v){} } and I want to only allow access to go with a finite set of parameters. My first thought was this: Struct { private void goT(T)(T v){} public alias goT!(int) go; public alias goT!(char) go; public alias goT!(byte) go; public alias goT!(float) go; } But you cant tunnel through private with a public alias. Does anyone known of a clean get somthing like this? Wrapper functions are not an option. |
May 18, 2007 Re: can I get public alias to private templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> I have a struct like this
>
> Struct
> {
> void go(T)(T v){}
> }
>
> and I want to only allow access to go with a finite set of parameters. My first thought was this:
>
> Struct
> {
> private void goT(T)(T v){}
> public alias goT!(int) go;
> public alias goT!(char) go;
> public alias goT!(byte) go;
> public alias goT!(float) go;
> }
>
> But you cant tunnel through private with a public alias. Does anyone known of a clean get somthing like this? Wrapper functions are not an option.
>
>
I don't know of any way to expose public names for private things in quite that way, but you might consider something like this:
private void goT(T)(T v){
static if (!(is(T == int) || is(T == char) || is(T == byte) || is(T == float))) {
static assert (false, "...useful message...");
}
// ... code ...
}
Not super pretty, I know, but it should work. It would be nice if there were a cleaner way of doing this for those cases where the list is long. (I have something I would've liked to templatize in a similar fashion, but with about 10 types to support.)
-- Chris Nicholson-Sauls
|
May 18, 2007 Re: can I get public alias to private templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | Reply to Chris Nicholson-Sauls,
> I don't know of any way to expose public names for private things in
> quite that way, but you might consider something like this:
>
> private void goT(T)(T v){
> static if (!(is(T == int) || is(T == char) || is(T == byte) || is(T
> == float))) {
> static assert (false, "...useful message...");
> }
> // ... code ...
> }
> Not super pretty, I know, but it should work. It would be nice if
> there were a cleaner way of doing this for those cases where the list
> is long. (I have something I would've liked to templatize in a
> similar fashion, but with about 10 types to support.)
>
Yeah, I guess I could do that. But I also have a bunch of other stuff that would have to be checked. I guess I could make it all public and then make threats against the lives on anyone who uses the template directly. :b
|
Copyright © 1999-2021 by the D Language Foundation