public alias to private template implementation

Meta jared771 at gmail.com
Mon Sep 16 16:53:13 PDT 2013


On Monday, 16 September 2013 at 21:00:21 UTC, monarch_dodra wrote:
> I'm trying to implement a set of public funtions, in terms of a 
> template. Said template has no business being know to the user, 
> so I want to mark it as private. Unfortunately, if I do this, 
> then I can't use the alias, because "Impl is private".
>
> Question 1: Is this the correct behavior? I'd have expected 
> that if my alias is public, it would allow any one from outside 
> to make the call correctly.
>
> Question 2: Is there a "correct" way to do this? I possible, 
> I'd want to avoid "nesting" the template, eg:
> auto fun(){return Impl!int();}
> As that would:
> a) require more typing ^^
> b) incur an extra function call in non-inline
> c) if at all possible, I actually need "fun" to be a template, 
> so that it can be inlined.

It makes sense to me, and seems like it would be analogous to 
returning a private member from a public method in structs or 
classes. However, since an alias rewritten to what it is aliased 
to (they completely disappear at compile time), I think your code 
would be equivalent to this:

//----
module A;

private template Impl(T)
{
     void Impl(){}
}

//----
module B; //I'm assuming that main is supposed to be in a 
different module?
import A;
void main()
{
     Impl!int();
}

So you're really directly accessing a private symbol. Perhaps a 
workaround could be something like this:

//----
module Test;
static const fun = &Impl!int;
private template Impl(T)
{
     void Impl(){}
}

//----
module main;
import Test;
void main()
{
     fun();
}

I had to do fun = &Impl!int because the compiler complained about 
fun = Impl!int.


More information about the Digitalmars-d-learn mailing list