模板上的宏

发布于 2024-12-04 17:26:05 字数 1366 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

(り薆情海 2024-12-11 17:26:05

宏参数列表之前没有空格!

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

No space before the macro argument list!

#define _BINARY_OP_ON_DATASET(OP_TYPE) ...
                            ^^^
锦爱 2024-12-11 17:26:05

宏参数列表之前有一个无效的空格,并且错过了后面的换行符的转义:

#define _BINARY_OP_ON_DATASET(OP_TYPE) \
    ...

You have an invalid space before the macro argument list and missed escaping the newline after it:

#define _BINARY_OP_ON_DATASET(OP_TYPE) \
    ...
雪化雨蝶 2024-12-11 17:26:05

删除宏名称与其参数之间的空格。

#define _BINARY_OP_ON_DATASET(OP_TYPE)

\ 添加到模板的第一行

#define _BINARY_OP_ON_DATASET(OP_TYPE)\

删除小错误,例如您在函数体第二行中忘记的 ;

result.push_back(data1.begin().val+data1.begin().val);

并且编译良好!

Remove the space between the macro name and it's arguments.

#define _BINARY_OP_ON_DATASET(OP_TYPE)

Add \ to the first line of your template

#define _BINARY_OP_ON_DATASET(OP_TYPE)\

Remove the minor errors, such as the ; you forgot in the second line of your function body

result.push_back(data1.begin().val+data1.begin().val);

And it compiles well!

謸气贵蔟 2024-12-11 17:26:05

我认为重复定义比宏定义更简单:)有时重复自己比寻求更高的打字效率更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文