這僅僅是僞代碼:如何匹配Rust宏中的表達式類型?
macro_rules! attribute {
$e: expr<f32> => { /* magical float stuff */ };
$e: expr<i64> => { /* mystical int stuff */ };
};
我想有根據,我傳遞給宏類型不同擴展的宏。
這是它如何工作在C++
template <typename T>
struct Attribute{ void operator(T)() {} };
template <>
struct Attribute<float> {
void operator(float)(float) { /* magical float stuff */ }
};
template <>
struct Attribute<long> {
void operator()(long) { /* mystical int stuff */ }
}
只有宏調度的語法,特點OTOH可以根據類型調度。考慮使用特質,或者在宏內部使用特徵。 – bluss