gpt4 book ai didi

c++ - 模板上的宏

转载 作者:太空狗 更新时间:2023-10-29 20:17:10 31 4
gpt4 key购买 nike

不确定我尝试做的事情是否不好,但这是我的问题:我有一些模板函数,比如

std::vector<T> operator - (const std::vector<T>& data1, const std::vector<T>& data2);
std::vector<T> operator * (const std::vector<T>& data1, const std::vector<T>& data2);
std::vector<T> operator & (const std::vector<T>& data1, const std::vector<T>& data2);

....等等。除了运算符之外,所有这些函数都具有完全相同的定义,所以我试图编写这样的宏

#define _BINARY_OP_ON_DATASET (OP_TYPE)
template <typename T> \
std::vector<T> operator OP_TYPE (const std::vector<T>& data1, const std::vector<T>& data2)\
{\
std::vector<T> result;\
result.push_back(data1.begin().val OP_TYPE data1.begin().val)/*sample implementation*/\
return result;\
}

_BINARY_OP_ON_DATASET (&)
_BINARY_OP_ON_DATASET (+)

我得到了一堆错误

Error   1   error C2833: 'operator OP_TYPE' is not a recognized operator or type
Error 2 error C2988: unrecognizable template declaration/definition
Error 3 error C2059: syntax error : 'newline'
Error 5 error C2143: syntax error : missing ';' before '<'
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

...还有更多谁能看出这有什么问题?

感谢您的帮助。

简历

最佳答案

宏参数列表前没有空格!

#define _BINARY_OP_ON_DATASET(OP_TYPE) ...
^^^

关于c++ - 模板上的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7447016/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com