模板上的宏
不确定我想做的事情是否不好,但这是我的问题: 我有一些模板函数,例如
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
......等等 谁能看出这有什么问题吗?
感谢您的任何帮助。
简历
Not sure if what am trying to do is bad but here's my question:
I have a some template functions like
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);
....and so on. All of these functions have exactly the same definition except for the operator, so I was trying to write a macro like this
#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 (+)
And I get a bunch of errors
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
...and more
Can anyone see whats the problem with this?
Thanks for any help.
CV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
宏参数列表之前没有空格!
No space before the macro argument list!
宏参数列表之前有一个无效的空格,并且错过了后面的换行符的转义:
You have an invalid space before the macro argument list and missed escaping the newline after it:
删除宏名称与其参数之间的空格。
将
\
添加到模板的第一行删除小错误,例如您在函数体第二行中忘记的
;
并且编译良好!
Remove the space between the macro name and it's arguments.
Add
\
to the first line of your templateRemove the minor errors, such as the
;
you forgot in the second line of your function bodyAnd it compiles well!
我认为重复定义比宏定义更简单:)有时重复自己比寻求更高的打字效率更好。
I think repeating the definition will be simpler than macro-fying them :) Sometimes it's just better to repeat yourself than to eek out more typing efficiency.