2012-06-11 21 views
7

BOOST_CHECK_EQUAL (जोड़ी, जोड़ी) करने का प्रयास करते समय, gcc को जोड़ी के लिए स्ट्रीम ऑपरेटर नहीं मिला है, इसे घोषित करने के बावजूद। मजाकिया बात यह है कि std :: out ऑपरेटर पाता है।जोड़ी <int, int> और कस्टम ऑपरेटर के साथ BOOST_CHECK_EQUAL <<

ostream& operator<<(ostream& s, const pair<int,int>& p) { 
    s << '<' << p.first << ',' << p.second << '>'; 
    return s; 
} 


BOOST_AUTO_TEST_CASE(works) 
{ 
    pair<int,int> expected(5, 5); 
    pair<int,int> actual (5, 5); 
    std::cout << expected << std::endl; 
    std::cout << actual << std::endl; 
    BOOST_CHECK(actual == expected); 
} 

BOOST_AUTO_TEST_CASE(no_work) 
{ 
    pair<int,int> expected(5, 5); 
    pair<int,int> actual (5, 5); 
    BOOST_CHECK_EQUAL(actual, expected); 
} 

यह does not त्रुटि के साथ संकलन:

... instantiated from here 
../boost-atp/release/include/boost/test/test_tools.hpp:326:9: error: no match for ‘operator<<’ in ‘ostr << t’ 
+0

कस्टम प्रकार के लिए अपने स्वयं के मुद्रण को परिभाषित करने की * नई * जिस तरह से यहां समझाया गया है: https://stackoverflow.com/a/44810846/1617295 और [यह है आधिकारिक दस्तावेज] (उस सुविधा के http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/test_output/test_tools_support_for_logging/testing_tool_output_disable.html)। – Raffi

+0

@Raffi यह प्रश्न इस के डुप्लिकेट की तरह दिखता है, इसमें कम विवरण है। हो सकता है कि इसे डुप्लिकेट के रूप में चिह्नित करना और अपना उत्तर यहां ले जाएं ताकि हम उपयोगकर्ताओं को कूद न सकें? मैं इसे भी स्वीकार कर सकता हूं। धन्यवाद! – nishantjr

उत्तर

7

operator<<stdRemus's answer 0 +3514 ड्राफ्ट (एन 4296 अनुभाग: 17.6.4.2.1) में अपरिभाषित व्यवहार है। बूस्ट एक हुक (used by this answer) प्रदान करता है और आप लिख सकते हैं:

namespace boost 
{ 
    namespace test_tools 
    { 
     template<typename T,typename U> 
     struct print_log_value<std::pair<T, U> > 
     { 
      void operator()(std::ostream& os, std::pair<T, U> const& pr) 
      { 
       os << "<" << std::get<0>(pr) << "," << std::get<1>(pr) << ">"; 
      } 
     }; 
    } 
} 

print_log_value एक टेम्पलेट है, इसलिए यदि आप pair<T,U> की तरह एक टेम्प्लेटेड मूल्य घोषित नहीं कर रहे हैं, तो आप की तरह कुछ लिखने के लिए की आवश्यकता होगी:

template<> 
struct print_log_value<MyType>{ /* implementation here*/ }; 

संपादित करें

यदि आप बूस्ट 1.5 9 या बाद में उपयोग कर रहे हैं तो आपको इसके बजाय नामस्थान boost::test_tools::tt_detail का उपयोग करने की आवश्यकता है। यही कारण है, कोड शुरू करने के लिए की जरूरत है:

namespace boost 
{ 
    namespace test_tools 
    { 
     namespace tt_detail 
     { 
+0

आह! एक दम बढ़िया! यह बहुत साफ है। – nishantjr

9

डाल operator itself in the std namespace का प्रयास करें:

namespace std 
{ 
    ostream& operator<<(ostream& s, const pair<int,int>& p) { 
    s << '<' << p.first << ',' << p.second << '>'; 
    return s; 
    } 
} 

अद्यतन: शायद यही कारण है कि ADL fails (कम से कम LLVM पर):

Just like before, unqualified lookup didn't find any declarations with the name operator<< . Unlike before, the argument types both contain class types: one of them is an instance of the class template type std::basic_ostream , and the other is the type ns::Data that we declared above. Therefore, ADL will look in the namespaces std and ns for an operator<< . Since one of the argument types was still dependent during the template definition, ADL isn't done until the template is instantiated during Use, which means that the operator<< we want it to find has already been declared. Unfortunately, it was declared in the global namespace, not in either of the namespaces that ADL will look in!

+0

मुझे नहीं पता कि एडीएल BOOST_CHECK_EQUAL के भीतर क्यों लागू नहीं होता है। क्या बूस्ट ऐसा होने से रोकने के लिए कुछ करता है? – nishantjr

+0

@ एनजेआर: मैंने इसे कुछ समय पहले देखा लेकिन मूल कारण नहीं मिला। अफैक जीसीसी एडीएल करता है, और ऐसा भी करता है। –

+1

यह मेरे लिए काम करता है, लेकिन ऐसा लगता है कि अपरिभाषित व्यवहार (http://en.cppreference.com/w/cpp/language/extending_std) है। देख रहे हैं, मैंने निम्न उत्तर खोजा है जो अपरिभाषित व्यवहार से बचने का एक तरीका देता है http://stackoverflow.com/a/17573165/309334 – Eponymous

0

मैं हेक्स में पूर्णांक प्रिंट करने के लिए आउटपुट स्ट्रिंग को कस्टमाइज़ करने का एक तरीका, इसी तरह की तलाश कर रहा था। एसडीडी नेमस्पेस में ऑपरेटर इंजेक्शन करना काम करेगा, लेकिन मेरे परीक्षण में प्रत्येक BOOST_CHECK हेक्स में मुद्रित किया जाएगा।

इसलिए मैंने बूस्ट नेमस्पेस में कुछ कस्टम ऑपरेटरों को इंजेक्शन दिया कि मैं कुछ वैश्विक बूलों के साथ नियंत्रण कर सकता हूं।

मेरा उत्तर यहां देखें boost-check-fails-to-compile-operator-for-custom-types

संबंधित मुद्दे