2013-06-10 6 views
4

술어 자체가 템플릿 함수 인 BOOST_CHECK_PREDICATE에 대한 사용자 정의 술어를 작성하려고합니다.템플릿 함수를 Boost :: Unit-test에 대한 사용자 정의 술어로 사용하는 방법

3> .. \ boost_test testSystem.cpp (42) \ : 오류 C2780 : ' 을 bool에 2010 ++ MS 비주얼 C로 컴파일

#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() 

다음과 같은 오류를 제공합니다 다음과 같이 내 예를 보인다 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\ 부스트 \ 포함 \ : - 80,403,210, CONST 숯 * CONST ARG2 &, CONST 숯불 는 * CONST에서 arg3 &, CONST 숯 * CONST Arg4 &, CONST 숯 *)는 '10 3>
C 제공된 16 개 인수 기대 boost :: test_tools \ testSystem.cpp (42) : 오류 C2780 : 'boost :: test_tools :: tt_detail :: check_frwd' 선언의3> .. \ boost_test \ testSystem.cpp 선언을 참조하십시오. 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 *, co nst Arg1 &, const char *, const Arg2 &, const char *, const Arg3 &, const char *) ': 예상되는 14 개의 인수 -10 개 3 C : \ Boost \ include \ boost-1_51 \ boost/test/test_tools.hpp (523) : 'boost :: test_tools :: tt_detail :: check_frwd'의 선언을 참조하십시오. 3> .. \ boost_test \ testSystem.cpp (42) : 오류 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 Arg2C : \ Boost \ include \ boost-1_51 \ boost/test/test_tools.hpp (523) : 의 'boost :: test_tools :: 선언'을 참조하십시오. tt_detail :: check_frwd ' 3> ... \ boost_test testSystem.cpp \ (42) : 오류 C2896' 부스트 bool에 :: test_tools :: tt_detail :: check_frwd (Pred를 가산하여, 부스트 : CONST unit_test :: lazy_ostream & boost :: test_tools :: const_string, size_t, boost :: test_tools :: tt_detail :: tool_level, 부스트 :: test_tools :: tt_detail :: check_type, const Arg0 &, const char *, const Arg1 &, const char *) ': 함수 템플릿을 사용할 수 없습니다'bool is_close_enough (const U &, const V &) '함수로인수 3> .. \ boost_test \ testSystem.cpp (18) : 선언'is_close_enough '3> .. \ boost_test \ testSystem을 참조하십시오.cpp (42) : 오류 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 *) ':'오버로드 된 함수 유형 '에 대한 템플릿 인수가 에서 추론 할 수 없습니다. 3 ':: test_tools :: tt_detail :: check_frwd 향상'을의 선언을 참조하십시오 부스트 1_51 \ 부스트/테스트/test_tools.hpp (523) \ 포함 \ 부스트 \ 3>
C ' 기능 유형을 오버로드' > .. \ boost_test \ testSystem.cpp (42) : 오류 C2780 : 'bool boost :: test_tools :: tt_detail :: check_frwd (Pred, const boost :: unit_test :: lazy_ostream &, boost :: test_tools :: const_string, size_t, 부스트 :: test_tools :: tt_detail :: tool_level, boost :: test_tools :: tt_detail :: check_type, const를 는 arg0 &, CONST의 char *)는 '8 개 인수 기대 - 10 내가 잘못 여기서 뭘하는지

어떤 아이디어를 제공?

답변

2

다음은 Visual Studio 2012 및 g ++ 4.8.1의 Boost 1.53.0에서 잘 작동합니다. 나는 템플릿 함수를 사용하기를 원한다면 템플릿 매개 변수를 명시 적으로 지정할 필요가 있다고 생각한다. 이런 이유로 저는 functor를 가진 솔루션을 선호합니다.

#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() 
+0

빠른 답장을 보내 주셔서 감사합니다! 솔루션은 나를 위해 잘 작동합니다. – user1832210