C ++域特定的嵌入式语言运算符 [英] C++ domain specific embedded language operators
问题描述
在数值语言(Matlab,Fortran)中,范围操作符和语义在使用多维数据时非常方便。
例如:
A(i:j,k,:n)//表示二维切片B (c:\\ p $ p>
声明为
不幸的是C ++没有范围操作符(:)。当然它可以使用范围/切片函子来模拟,但语义比Matlab更干净。我是C ++中的原型矩阵/张量域语言,我想知道是否有任何选项来重现范围运算符。
我仍然想依赖于C ++ / prprocessor框架。
到目前为止,我已经看过boost wave,这可能是一个合适的选项。
是否还有其他方法向C ++ DSL引入新的非本地操作符?
我知道你不能添加新的操作符.am专门寻找解决方法。
我发现了一件事(非常丑陋的黑客,我不打算使用):#define A r)A [range(((1)?r),((0)?r))] //假设A overloads []
A(i:j) // abuse ternary operator
解决方案之前使用的是编写一个外部预处理器来解析源代码,并用vanilla C ++替换您的自定义运算符的任何使用。为了您的目的,
a:b
使用将替换为a.operator_range_(b)
c $ c> operator:()range_ operator_range _()
的声明。在您的makefile中,然后添加一个规则,在编译源文件之前对其进行预处理。这可以在Perl中相对轻松地完成。
但是,在过去使用类似的解决方案,我不推荐它。如果您不小心处理和生成源代码,则可能会产生可维护性和可移植性问题。
In numerical oriented languages (Matlab, Fortran) range operator and semantics is very handy when working with multidimensional data. For example:
A(i:j,k,:n) // represents two-dimensional slice B(i:j,0:n) of A at index k
unfortunately C++ does not have range operator (:). of course it can be emulated using range/slice functor, but semantics is less clean than Matlab. I am prototyping matrix/tensor domain language in C++ and am wondering if there any options to reproduce range operator. I still would like to rely on C++/prprocessor framework exclusively.
So far I have looked through boost wave which might be an suitable option.
is there any other means to introduce new non-native operators to C++ DSL?
I know you cannot add new operators.am specifically looking for workaround. One thing I came up (very ugly hack and I do not intend to use):
#define A(r) A[range(((1)?r), ((0)?r))] // assume A overloads [] A(i:j); // abuse ternary operator
解决方案A solution that I've used before is to write an external preprocessor that parses the source and replaces any uses of your custom operator with vanilla C++. For your purposes,
a : b
uses would be replaced with something likea.operator_range_(b)
, andoperator:()
declarations with declarations ofrange_ operator_range_()
. In your makefile you then add a rule that preprocesses source files before compiling them. This can be done with relative ease in Perl.However, having worked with a similar solution in the past, I do not recommend it. It has the potential to create maintainability and portability issues if you do not remain vigilant of how source is processed and generated.
这篇关于C ++域特定的嵌入式语言运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!