iT邦幫忙

0

在cppsp使用@custom,類似c/c++巨集但更安全和命名隔離

  • 分享至 

  • xImage
  •  
  • @custom xxx("...",<{...}>,...): @custom可以讓使用者自訂語法,允許巢狀函數呼叫和單層命名空間隔離,"..."內容會變成產生的程式碼,<{...}>與其類似但它會被當成佔位符號,並在呼叫時被參數替換。最多只能用一層namespace{}例如namespace n{ @custom.... }
import iostream,vector
 @function<< class B{int aa;};>>
namespace fromcpp{
@custom  tem("template<typename T> ")
struct T  
//use struct to declare a type
}
 //c++ 泛型
  fromcpp.tem()
function add(T a,T b) T{return a+b}
  //Generics with c++ template
@custom cs("< ",<{T}>," >")
 print(add cs(int) (1,2) )
 add cs(int)  (1,2) 

 
 //用@custom 產生STL容器
@custom vec("std::vector<",<{T}>,"> ")
@custom decl(<{name}>,";")
@custom def(<{name}>,"=",<{value}>,";")
vec(int) ;decl(a)
vec(vec(char)); def(b,{{'a','b','c','d','e'}})
 //cppsp 風格泛型方法之一
@custom subs(<{T}>," sub(",<{T a}>,",",<{T b}>,")"," {return a-b;}")
subs(int ,int a,int b)
function [sub]
print("\n",sub(3,4))
 @custom class("class ",<{T}>,"{",<{body}>,"};")
 class(obja,var a int
  var s,ss string
  var f float
 
 )
@custom auto("auto ",<{name}>," = [",<{cap}>,"]","(",<{param}>,")","{",<{body}>,";};")
if(true){
    auto(x,&,int a,return a+1)
    function [x]
    print("\n auto:",x(9))
}

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言