yanyiwu / cppjieba

"结巴"中文分词的C++版本
MIT License
2.57k stars 690 forks source link

宏定义 print(x) 设计问题,与第三方库冲突 #101

Open RabbitLee opened 6 years ago

RabbitLee commented 6 years ago

https://github.com/yanyiwu/cppjieba/blob/6aff1f637c784c27af6bb0868a94ba22617e65b0/deps/limonp/StdExtension.hpp#L32

我猜测这个宏是为了方便,但是使用宏定义这种方式在这里是不是不如定义为一个函数更为通用和健壮?使用宏会与其他代码有所冲突: 我在项目中引入了两个外部库 cppjieba 和语言模型 srilm,在编译时出现了如下错误

/home/user/cppjieba/deps/limonp/StdExtension.hpp:32:23: error: invalid use of ‘::’
 #define print(x) std::cout << x << std::endl
                       ^
/home/user/srilm/include/MemStats.h:35:11: note: in expansion of macro ‘print’
  ostream &print(ostream &stream = cerr);

在这里 srilm 中定义的 print 成员函数被宏替换,会导致原有的函数失效甚至语法错误,如果当时没有使用宏而是定义了一个函数是不是就没有此类冲突问题了呢?

谢谢!

ZhengfengRao commented 5 years ago

计算ppl?我也在这么用。。。 遇到了相同的问题,把这行注释掉就行了

byronhe commented 5 years ago

https://github.com/yanyiwu/cppjieba/commit/f55b591968ee9d440efccfbfbeb61c516385f607 已经 fix 了

github-actions[bot] commented 1 week ago

This issue has not been updated for over 5 years and will be marked as stale. If the issue still exists, please comment or update the issue, otherwise it will be closed after 7 days.