0

What is the best way to resolve a conflicting macro name with a boost template function? After including boost/chrono.hpp I got the compiler error:

CCN5816 (W) Too many arguments are specified for the macro "round". The extra arguments are ignored. "/usr/include/math.h", line 2133.16: CCN5425 (I) "round" is defined on line 2133 of "/usr/include/math.h".

round conflicts with the template function 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)
    {
  • It seems that I can either #undef the macro if I patch round.hpp
  • Maybe I can use parantheses? In the answer to macro and member function conflict the function name is within parentheses but this is in the usage of the function, not in its definition. Would it work in the definition too?

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));
}
Community
  • 1
  • 1
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • 2
    Ha, this seems like a classic example why not to use macros, but you can't tell the standard library that, I figure... – cadaniluk Feb 11 '17 at 21:47
  • @Downvoter Indeed, and quite bad form on the libraries writer's part to destroy such a common name as `round`. – Paul Evans Feb 11 '17 at 22:40

1 Answers1

-1

Simply, always including <gtest/gtest.h> after <boost/chrono.hpp>: that way the #define round ... won't affect the boost code.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54