boost.test:在测试套件中包含STL代码时编译错误。

huangapple go评论66阅读模式
英文:

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

问题

以下是翻译好的内容:

我的设置:

我的测试文件:

// 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() // (*)

然后我尝试编译如下:
g++ test.cpp -o test.exe

但出现了大量错误(精确到>1700行)。以下是开头部分的一些错误:

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>
      |                        ^

以及底部的一些错误:

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>}

如果删除标有星号的行(即与 BOOST_AUTO_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?

答案1

得分: 3

The BOOST_AUTO_TEST_SUITE宏引入了一个命名空间。标准库头文件只能包含在全局命名空间中,不能在其他地方包含。

这是一个示例,除了标准库之外不使用任何库,这应该导致类似于您的编译错误:

#define ADD_NAMESPACE 1

#if ADD_NAMESPACE
namespace Foo
{
#endif

#include <chrono>

#if ADD_NAMESPACE
}
#endif

在线演示

(将 #define ADD_NAMESPACE 0 改为使编译正常工作。)

只需将包含部分移动即可正常工作。

#include <chrono>

#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE a_nice_testing_attempt
#include <boost/test/included/unit_test.hpp>  // 这是可用的

BOOST_AUTO_TEST_SUITE(MyNiceTestSuite)  // (*)
// 实际上我想测试更多东西,但我将问题缩小到这个最小的代码段

BOOST_AUTO_TEST_SUITE_END() // (*)
英文:

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 &lt;chrono&gt;

#if ADD_NAMESPACE
}
#endif

Godbolt Demo

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

Simply moving the include should work.

#include &lt;chrono&gt;

#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE a_nice_testing_attempt
#include &lt;boost/test/included/unit_test.hpp&gt;  // 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() // (*)

huangapple
  • 本文由 发表于 2023年6月8日 00:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定