gpt4 book ai didi

c++ - 如何在 C++ 中将值与模板和 boost 进行比较?

转载 作者:行者123 更新时间:2023-11-28 04:18:48 24 4
gpt4 key购买 nike

我创建了一个模板函数,可以在其中放置每种类型。现在确实该功能将始终有效。如果类型 T 是 int,则 int 总是来自函数。如果一个字符串进来,一个字符串就出来。

现在我刚刚查看了 boost 文档。 Boost 指示我必须使用 BOOST_AUTO_TEST_CASE_TEMPLATE。

假设我有以下情况:

BOOST_AUTO_TEST_SUITE (MyTestSuite)

typedef boost :: mpl :: list <int, long, short, double, float, std :: string> TestTypes;

BOOST_AUTO_TEST_CASE_TEMPLATE (MyTestCase, T, TestTypes)
{
// These three values ​​will be separated by ',' and can be accessed seprately thanks to their individual getters.
std :: string input = "5,6,7";
Myclass <T> myClass (input);

BOOST_TEST (myClass.getFirst() == 5);
BOOST_TEST (myClass.getSecond() == 6);
BOOST_TEST (myClass.getThird() == 7);
}

BOOST_AUTO_TEST_SUITE_END()

如你所见,我不想检查我取回的类型是否正确,而是我取回的值是否与我给的值相同。因此,如果我给出 5 并且类型为 double,那么我的类(class)为 5.00。我的支票目前不这样做。如何确保我要查询的号码始终与我提供的类型相同?这听起来可能有点困难,但如果您需要更多信息,那么我将编辑此消息。

假设这行得通。然后我想使用该类型的最小值和最大值作为输入。我怎么做?如何获取 T 的最小值和最大值?

最佳答案

您可以使用decltype 和 STL traits:

BOOST_CHECK(std::is_same<T, decltype(myClass.getFirst())>::value);

Then I want to use the minimum and maximum value of that type as input. How do I do that?

使用 std::numeric_limits

关于c++ - 如何在 C++ 中将值与模板和 boost 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55952764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com