Search code examples
c++c++11boostc++-chronoboost-test

boost.test: compile errors when STL code is included within a test suite


My setup:

My test file:

// test.cpp
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE a_nice_testing_attempt
#include <boost/test/included/unit_test.hpp>  // this is available

BOOST_AUTO_TEST_SUITE(MyNiceTestSuite)  // (*)

#include <chrono>
// actually I want to test much more, but I narrowed
// the problem down to this minimal snippet

BOOST_AUTO_TEST_SUITE_END() // (*)

Then I try to compile as: g++ test.cpp -o test.exe

And get a bunch of errors (>1700 lines to be precise). Here's some at the very top:

In file included from C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:37,
                 from C:/msys64/mingw64/include/c++/12.2.0/chrono:39,
                 from test_main.cpp:17:
C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected template-name before '<' token
   58 |     : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
      |                        ^
C:/msys64/mingw64/include/c++/12.2.0/ratio:58:24: error: expected '{' before '<' token
C:/msys64/mingw64/include/c++/12.2.0/ratio:63:24: error: expected template-name before '<' token
   63 |     : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
      |                        ^

And some from the bottom:

C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1371:47:   required from 'static MyNiceTestSuite:
:std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, _Dur> MyNiceTestSuite::std:
:filesystem::__file_clock::_S_from_sys(const MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuit
e::std::chrono::_V2::system_clock, _Dur2>&) [with _Dur = MyNiceTestSuite::std::chrono::duration<long
 long int, MyNiceTestSuite::std::ratio<1, 1000000000> >]'
C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1338:27:   required from here
C:/msys64/mingw64/include/c++/12.2.0/bits/chrono.h:1020:16: error: cannot convert '__time_point' {ak
a 'MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::std::filesystem::__file_clock, int>'} t
o 'int' in return
 1020 |         return __time_point(__lhs.time_since_epoch() -__rhs);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                |
      |                __time_point {aka MyNiceTestSuite::std::chrono::time_point<MyNiceTestSuite::s
td::filesystem::__file_clock, int>}

If I remove the lines marked with the astherisk (i.e. BOOST_AUTO_TEST_SUITE related), then it compiles without a problem. Unfortunately I am unable to progress from here and my question is rather vague:

  • what is going on here and how could I solve it?

Solution

  • The BOOST_AUTO_TEST_SUITE macro introduces a namespace. Standard library headers must not be included anywhere but in the global namespace.

    This is a example not using any libs other than the standard library which should result in compiler errors similar to yours:

    #define ADD_NAMESPACE 1
    
    #if ADD_NAMESPACE
    namespace Foo
    {
    #endif
    
    #include <chrono>
    
    #if ADD_NAMESPACE
    }
    #endif
    

    Godbolt Demo

    (change to #define ADD_NAMESPACE 0 to make the compilation work.)

    Simply moving the include should work.

    #include <chrono>
    
    #define BOOST_TEST_MAIN
    #define BOOST_TEST_MODULE a_nice_testing_attempt
    #include <boost/test/included/unit_test.hpp>  // this is available
    
    BOOST_AUTO_TEST_SUITE(MyNiceTestSuite)  // (*)
    // actually I want to test much more, but I narrowed
    // the problem down to this minimal snippet
    
    BOOST_AUTO_TEST_SUITE_END() // (*)