Partial template function specialization
Peter Alexander
peter.alexander.au at gmail.com
Thu Aug 20 07:22:00 PDT 2009
Lars T. Kyllingstad Wrote:
> Disclaimer: You didn't say whether you use D1 or D2, but I use D2, so
> I'll give my answer in D2 code. It is highly likely it will also work in D1.
I'm using neither :) I'm just considering learning at the moment.
> First of all, I don't know how it is in C++, but in D you rarely write
> function declarations without definitions. So unless you have a very
> general function body for your "general case", I'd simply drop it. If
> you have, the general case looks like this:
>
> <snip>
>
> For more info, check out http://www.digitalmars.com/d/2.0/template.html
>
> -Lars
Excellent! Thanks a lot. I was hoping that D could overcome this problem.
Ah, one (maybe) final question:
Is there an equivalent to friends in D (didn't see any in the docs)? If so, do they work with templates easily?
In my style of programming, I very rarely use member functions (I think they are an atrocity in language design), so I create global functions for almost everything, and when they need to access to private members, I have the class declare the function as a friend.
Does D support my style of programming?
Here's a concrete example of something I'd like to do (pseudocode):
class Foo { private int x; friend globalFun; }
class Bar { private int y; friend globalFun; }
void globalFun(ref Foo foo, ref Bar bar) { foo.x = bar.y; }
Is there anything like this in D? Export sounds like the right thing, but can that be used in the example above, assuming that Foo and Bar are in separate modules?
More information about the Digitalmars-d-learn
mailing list