Metaprogramming in C++ and in D

后端 未结 10 1931
生来不讨喜
生来不讨喜 2021-01-30 04:46

The template mechanism in C++ only accidentally became useful for template metaprogramming. On the other hand, D\'s was designed specifically to facilitate this. And apparently

10条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 05:33

    in D you can check the size of a type and the available methods on it and decide which implementation you want to use

    this is used for example in the core.atomic module

    bool cas(T,V1,V2)( shared(T)* here, const V1 ifThis, const V2 writeThis ){
        static if(T.sizeof == byte.sizeof){
           //do 1 byte CaS
        }else static if(T.sizeof == short.sizeof){
           //do 2 byte CaS
        }else static if( T.sizeof == int.sizeof ){
           //do 4 byte CaS
        }else static if( T.sizeof == long.sizeof ){
           //do 8 byte CaS
        }else static assert(false);
    }
    

提交回复
热议问题