Thread overview
How to check that function gets ref parameter?
Dec 13
Basile B.
December 13
I'm trying to check that function has 'ref' parameter. The only way I found so far is to use std.traits.Parameters.

Here is the code I have:
    void f(int) {}
    void g(ref int) {}
    void main()
    {
        writeln(Parameters!f[0].stringof);
        writeln(__traits(isRef, Parameters!f[0]));
        writeln(Parameters!g.stringof);
        writeln(Parameters!g[0].stringof);
        writeln(__traits(isRef, Parameters!g[0]));
    }

The output is:
    int
    false
    (ref int)
    int
    false

"Parameters!g[0]" strips off 'ref' qualifier as you can see. How can I make it work?
December 13
On Sunday, 13 December 2020 at 16:41:06 UTC, Andrey Zherikov wrote:
> I'm trying to check that function has 'ref' parameter. The only way I found so far is to use std.traits.Parameters.
>
> Here is the code I have:
>     void f(int) {}
>     void g(ref int) {}
>     void main()
>     {
>         writeln(Parameters!f[0].stringof);
>         writeln(__traits(isRef, Parameters!f[0]));
>         writeln(Parameters!g.stringof);
>         writeln(Parameters!g[0].stringof);
>         writeln(__traits(isRef, Parameters!g[0]));
>     }
>
> The output is:
>     int
>     false
>     (ref int)
>     int
>     false
>
> "Parameters!g[0]" strips off 'ref' qualifier as you can see. How can I make it work?

You use the wrong traits, try ParameterStorageClassTuple instead:

  void f(int) {}
  void g(ref int) {}
  import std.traits : ParameterStorageClass, ParameterStorageClassTuple;
  static assert(ParameterStorageClassTuple!g[0] == ParameterStorageClass.ref_);
  static assert(ParameterStorageClassTuple!f[0] != ParameterStorageClass.ref_);


December 14
On Sunday, 13 December 2020 at 19:49:35 UTC, Basile B. wrote:
> On Sunday, 13 December 2020 at 16:41:06 UTC, Andrey Zherikov wrote:
> You use the wrong traits, try ParameterStorageClassTuple instead:
>
>   void f(int) {}
>   void g(ref int) {}
>   import std.traits : ParameterStorageClass, ParameterStorageClassTuple;
>   static assert(ParameterStorageClassTuple!g[0] == ParameterStorageClass.ref_);
>   static assert(ParameterStorageClassTuple!f[0] != ParameterStorageClass.ref_);

This works. Thanks!