Is it possible to "overload" based on visibility?

H. S. Teoh hsteoh at
Fri Sep 25 18:58:54 UTC 2020

On Fri, Sep 25, 2020 at 05:58:08PM +0000, 60rntogo via Digitalmars-d-learn wrote:
> On Friday, 25 September 2020 at 15:21:22 UTC, Steven Schveighoffer wrote:
> > If the input is not ref, you should not return by ref, because then
> > you would be returning a reference to local stack data that is about
> > to be destroyed.
> Yes, I understand that. What I'm really after at this point is that I
> would like to write a clever mixin that would handle all of these
> decisions for me. It should generate a function that takes arguments
> and returns the result by value or const reference depending on what
> is more appropriate for the given types. I was under the impression
> that this could be accomplished using in or some other qualifiers.

You probably need to use the long-form of templates, with separate
function declarations, to accomplish this. E.g.:

	template myFunc(Args...) {
		static if (shouldReturnByRef!Args)
			ref ReturnType myFunc(Args args) {
				... // implementation here
		else // non-ref return
			ReturnType myFunc(Args args) {
				... // implementation here

The reason is that `ref` return is an attribute of the *function*, not
the return type, so you can't just use an `auto` return type and have
the compiler infer it.


What doesn't kill me makes me stranger.

More information about the Digitalmars-d-learn mailing list