当前位置:网站首页>Distribution operation of D

Distribution operation of D

2022-06-26 06:27:00 fqbqrr

original text

void foo(){
    }

  struct A{
    
    auto opDispatch(string op)() {
    
      writeln;// deliberately , Do not import .
    }
  }

  void test(){
    
    A.init.foo();
  }
//dmd -c -o- main.d

When opDispatch When not compiling , happen . Correct behavior , It should not be parsed Writing lines function .
use void foo(A a){} Replace void fun(){}, Should not compile . But compiled .


opDispatch When the document does not state that it cannot be compiled , What to do .
We should pay attention to ,opDispatch Instead of ufcs.


Recently . I've been looking for explicit calls opDispatch Reason for not working :

A.init.opDispatch!"foo"();

Even if not compiled ( Then print the error message ), If nothing else Valid member , The application of opDispatch.
Possible uses are as follows :

struct A
{
    
   T wrapped;
   auto opDispatch(string m, Args...)(Args args) {
    
       mixin("return wrapped." ~ m ~ "(args);");
   }
}

It's not necessary constraint To ensure that , But the cost is high ( confused / Misleading error ).
opDispatch whether * matching * The principal should not be relied upon , And only rely on Signatures and constraints , Consistent with function template .


If you don't realize opDispatch, It is impossible to debug .
Refuse Instantiate instead of generate compilation errors , Can lead to error heavy load , Even the depth template in the depth domain fails . It's hard to track , The final discovery is opDispatch Target , But because of Suppress errors , and Skip quietly .
Should not rely on Realization And choose opDispatch. The error should be Hard compilation error . Yes forward Templates , It's easy to be in opDispatch Used in signature __traits(hasMember), To decide whether to instantiate . A small cost , And greatly improve Distribution operations Error report for .


In the past , Well done .

原网站

版权声明
本文为[fqbqrr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171325484457.html