| |
| Posted by Stanislav Blinov in reply to Ali Çehreli | PermalinkReply |
|
Stanislav Blinov
Posted in reply to Ali Çehreli
| On Tuesday, 28 December 2021 at 22:30:30 UTC, Ali Çehreli wrote:
> On 12/28/21 2:06 PM, Steven Schveighoffer wrote:
>
>>> void print_num(int mul)(int num) {
>
> Wasn't there a way of telling whether an 'auto ref' parameter is copied or not?
>
> void print_num()(int num, auto ref int mul) {
> // ?
> }
>
> And that would indicate that the argument was an rvalue?
__traits(isRef, mul).
> I realize that rvalues are not exactly what the OP is interested in.
Yup, different thing.
One can also do this kind of stuff:
```d
import core.stdc.stdio;
struct Literal(alias val)
{
enum value = val;
}
enum lit(alias val) = Literal!val.init;
void print_num(Arg)(int num, Arg mul)
{
static if (is(Arg == Literal!val, alias val))
{
static if (is(typeof(val) == string))
printf("mul by compile-time string \"%s\"!\n", val.ptr);
else static if (is(typeof(val) == int) && (val == 3))
printf("mul by compile-time 3!\n");
else
printf("mul by compile-time thing\n");
}
else
{
printf("mul by runtime thing\n");
}
}
void main()
{
print_num(10, lit!"hello"); // mul by compile-time string "hello"!
print_num(10, lit!3); // mul by compile-time 3!
print_num(10, lit!'a'); // mul by compile-time thing
print_num(10, 10); // mul by runtime thing
}
```
|