0

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?
deponovo
  • 1,114
  • 7
  • 23

1 Answers1

3

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() // (*)
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Awesome. I am failing to find the documentation for this. Is there some reference you could append to or link in your answer? Btw, edited the title to better match the problem on a generic level. – deponovo Jun 08 '23 at 06:45
  • @deponovo The documentation only seems to mention that the macro "works **very similar to** a namespace" which isn't exactly stating this properly. The only option here seems to be to take a look at the defines related to the macro definition in the source code ( https://github.com/boostorg/test/blob/9286fb0c0fe3bd6ac9a6c496735efa30865e63ac/include/boost/test/unit_test_suite.hpp#L59 ) or alternatively use functionality of an IDE providing a preview of the macro expansion. (Or having the compiler run the preprocessor only, but that will result 10k-s LOC to look through.) – fabian Jun 08 '23 at 06:56