c – 如何将模板化函数用作Boost :: Unit-test的自定义谓词

我正在尝试为BOOST_CHECK_PREDICATE构建自定义谓词,其中谓词本身是模板化函数.我的例子如下:

#define BOOST_TEST_MODULE Module
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

// custom predicate
template <typename U, typename V>
bool is_close_enough(const U& a, const V& b)
{
    return std::abs(a-b) < 2.0;
}

BOOST_AUTO_TEST_SUITE(boostUnitTestLearningTests)

BOOST_AUTO_TEST_CASE(Test_Templated_Predicate)
{
    BOOST_CHECK_PREDICATE(is_close_enough, (4)(6));
    BOOST_CHECK_PREDICATE(is_template_close_enough, (4.0)(6.5));
}

BOOST_AUTO_TEST_SUITE_END()

使用MS Visual C 2010进行编译会出现以下错误:

3>..\boost_test\testSystem.cpp(42): error C2780: ‘bool
boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char
*,const Arg3 &,const char *,const Arg4 &,const char *)’ : expects 16 arguments – 10 provided 3>
C:\Boost\include\boost-1_51\boost/test/test_tools.hpp(523) : see
declaration of ‘boost::test_tools::tt_detail::check_frwd’
3>..\boost_test\testSystem.cpp(42): error C2780: ‘bool
boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char
*,const Arg3 &,const char *)’ : expects 14 arguments – 10 provided 3> C:\Boost\include\boost-1_51\boost/test/test_tools.hpp(523) : see
declaration of ‘boost::test_tools::tt_detail::check_frwd’
3>..\boost_test\testSystem.cpp(42): error C2780: ‘bool
boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *,const Arg1 &,const char *,const Arg2 &,const char
*)’ : expects 12 arguments – 10 provided 3> C:\Boost\include\boost-1_51\boost/test/test_tools.hpp(523) : see
declaration of ‘boost::test_tools::tt_detail::check_frwd’
3>..\boost_test\testSystem.cpp(42): error C2896: ‘bool
boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *,const Arg1 &,const char *)’ : cannot use function
template ‘bool is_close_enough(const U &,const V &)’ as a function
argument 3> ..\boost_test\testSystem.cpp(18) : see
declaration of ‘is_close_enough’ 3>..\boost_test\testSystem.cpp(42):
error C2784: ‘bool boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *,const Arg1 &,const char *)’ : could not deduce
template argument for ‘overloaded function type’ from ‘overloaded
function type’ 3>
C:\Boost\include\boost-1_51\boost/test/test_tools.hpp(523) : see
declaration of ‘boost::test_tools::tt_detail::check_frwd’
3>..\boost_test\testSystem.cpp(42): error C2780: ‘bool
boost::test_tools::tt_detail::check_frwd(Pred,const
boost::unit_test::lazy_ostream
&,boost::test_tools::const_string,size_t,boost::test_tools::tt_detail::tool_level,boost::test_tools::tt_detail::check_type,const
Arg0 &,const char *)’ : expects 8 arguments – 10 provided

我在这里做错了什么想法?

最佳答案 以下适用于Visual Studio 2012和g 4.8.1上的Boost 1.53.0.我认为如果你想使用模板化函数,你需要明确指定模板参数.出于这个原因,我更喜欢带有仿函数的解决方案.

#define BOOST_TEST_MODULE Module
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>


template <typename U, typename V>
bool is_close_enough(const U& a, const V& b)
{
    return std::abs(a-b) < 2.0;
}

struct is_close_enough_functor
{
    template <typename U, typename V>
    bool operator()(const U& a, const V& b) const
    {
        return std::abs(a-b) < 2.0;
    }
};


BOOST_AUTO_TEST_SUITE(boostUnitTestLearningTests)

BOOST_AUTO_TEST_CASE(Test_Templated_Predicate)
{
    BOOST_CHECK_PREDICATE((is_close_enough<int,int>), (4)(6)); //extra parentheses needed to avoid a problem with the comma inside the macro
    BOOST_CHECK_PREDICATE(is_close_enough_functor(), (4)(6));
    BOOST_CHECK_PREDICATE(is_close_enough_functor(), (4.0)(6.5));
}

BOOST_AUTO_TEST_SUITE_END()
点赞