【问题标题】:How to resolve a name conflict between a system header macro and a boost template function?如何解决系统头宏和 boost 模板函数之间的名称冲突?
【发布时间】:2017-02-11 21:44:15
【问题描述】:

使用 boost 模板函数解决冲突的宏名称的最佳方法是什么? 包含 boost/chrono.hpp 后,我得到了编译器错误:

CCN5816 (W) 为宏“round”指定了太多参数。额外的参数被忽略。 "/usr/include/math.h", line 2133.16: CCN5425 (I) "round" 定义在 "/usr/include/math.h" 的第 2133 行。

round与模板函数boost::chrono::round冲突。

/usr/include/math.h

#define round(x)         __round(x)

/boost/chrono/round.hpp

namespace boost
{
  namespace chrono
  {

    /**
     * rounds to nearest, to even on tie
     */
    template <class To, class Rep, class Period>
    To round(const duration<Rep, Period>& d)
    {
  • 如果我修补round.hpp,我似乎可以#undef 宏
  • 也许我可以使用括号?在macro and member function conflict 的答案中,函数名称在括号内,但这是在函数的使用中,而不是在其定义中。它也适用于定义吗?

BoostChronoTest.cpp

#include <gtest/gtest.h>
#include <boost/chrono.hpp>

TEST(BoostChronoTest, simpleTests) {
    boost::chrono::nanoseconds ns(12000);

    // conversion with precision loss requires a cast
    boost::chrono::microseconds ms = boost::chrono::duration_cast<boost::chrono::microseconds>(ns);

    // no precision loss
    boost::chrono::nanoseconds  ns2 = ms;

    ms++;
    ms += boost::chrono::duration_cast<boost::chrono::microseconds>(ns);
    ns *= 2;
    EXPECT_TRUE(ms > ns);
    EXPECT_EQ(ms, boost::chrono::microseconds(25));
    EXPECT_EQ(ns, boost::chrono::nanoseconds(24000));
    EXPECT_EQ(ns2, boost::chrono::microseconds(12));
    EXPECT_EQ(ns2, boost::chrono::nanoseconds(12000));
}

【问题讨论】:

  • 哈,这似乎是一个经典的例子,为什么不使用宏,但你不能告诉标准库,我想......
  • @Downvoter 确实,图书馆作者破坏round这样的通用名称的形式非常糟糕。

标签: c++ boost xlc


【解决方案1】:

简单地说,总是包括&lt;gtest/gtest.h&gt; 之后 &lt;boost/chrono.hpp&gt;:这样#define round ... 就不会影响升压代码。

【讨论】:

  • 我没有包含 math.h 或 cmath,它们必须带有 gtest.h(我现在无法编译以验证其他包含顺序)
  • 感谢您的意见,我会尝试您的建议,如果可行,请发表评论
猜你喜欢
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多