boost::fusion
प्रलेखन से:
आई/ओ ऑपरेटरों नाम स्थान बढ़ावा में अतिभारित रहे हैं :: संलयन
जिसका मतलब है कि आप इन operator<<
का एक अंतर्निहित एकीकरण चाहते हैं, आपको अपने वर्तमान नेमस्पेस (::
) में boost::fusion
नेमस्पेस इंजेक्ट करने की आवश्यकता होगी, या स्पष्ट रूप से उनका उपयोग करें।
यह सब योग करने के लिए, जोड़ने:
using namespace boost::fusion;
अपने मामले में काम करना चाहिए। या एक स्पष्ट उपयोग के लिए, आप होगा लिखने के लिए:
boost::fusion::operator<<(std::cout, e) << std::endl;
--- संपादित करें ---
boost::fusion
के कोड थोड़ा पढ़ने के बाद, यह लगता है कि आप की वजह से उलझन में हैं boost::fusion::operators::operator<<
का चयन करें जो कि आपका तर्क वास्तविक boost::fusion::sequence
है।
यही कारण है कि आप boost::fusion
नाम स्थान इंजेक्षन की जरूरत नहीं है, और न ही स्पष्ट रूप से boost::fusion
नाम स्थान में परिभाषित प्रकार के लिए boost::fusion::operator<<
कहते हैं।
कुछ स्पष्टीकरण:
मैं कोएनिग के देखने (यह भी Argument Dependent Lookup रूप में जाना - ADL) की पूरी अवधारणा की व्याख्या नहीं होगा यहाँ के बाद से उस बिंदु नहीं है, लेकिन मूल रूप से, यह कहा गया है कि मामले में आप एक प्रयोग कर रहे हैं वेरिएबल जिसका नाम किसी नामस्थान के अंदर है, फिर फ़ंक्शन लुकअप उस पैरामीटर के नामस्थान तक फैला हुआ है।
इस विशेष मामले में, boost/fusion/sequence/io/out.hpp
समेत boost::fusion::operator::operator<<
परिभाषित करेगा जिसे boost::fusion
नामस्थान में इंजेक्शन दिया जाएगा।
$ cat /usr/local/include/boost/fusion/sequence/io/out.hpp
[...]
namespace boost { namespace fusion
{
[...]
namespace operators
{
template <typename Sequence>
inline typename
boost::enable_if<
fusion::traits::is_sequence<Sequence>
, std::ostream&
>::type // this is just a SFINAE trick to ensure
// the function will only be selected for
// actual boost::fusion::sequence
operator<<(std::ostream& os, Sequence const& seq)
{
return fusion::out(os, seq); // this will print out the sequence
}
}
using operators::operator<<; // here the operator<< is injected
// in boost::fusion
}}
किoperator<<
मापदंडों जिसका प्रकारboost::fusion
नाम स्थान में हैं उचित अधिभार मिलेगा साथ का उपयोग कर कॉल इसका मतलब यह है।
इस नामस्थान में स्थित तर्कों का उपयोग करने वाले कॉल का उपयोग operator<<
(यह आपके उदाहरण में मामला है) के उचित अधिभार को हल करने में विफल हो जाएगा।
आप boost::fusion
नामस्थान में अपना प्रकार परिभाषित करके इसे देख सकते हैं।
namespace boost { namespace fusion {
struct employee
{
std::string name;
int age;
};
}}
BOOST_FUSION_ADAPT_STRUCT(
boost::fusion::employee,
(std::string, name)
(int, age))
[...]
boost::fusion::employee e;
std::cout << e << std::endl; // ADL will work here
साइड नोट: यदि आप नाम देखने मुद्दों इस तरह डिबग करने के लिए चाहते हैं, आप gdb
उपयोग करना चाहिए। इस तरह आप हमेशा जानते होंगे कि कौन सा अधिभार चुना गया था। इस मामले में:
$ cat fusion.cpp
#include <iostream>
#include <cstdlib>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/sequence/io.hpp>
int main(int, char**)
{
boost::fusion::vector<int, char> foo(42, '?');
std::cout << foo << std::endl;
return EXIT_SUCCESS;
}
$ gdb -q ./fusion
Reading symbols for shared libraries ... done
(gdb) b 10
Breakpoint 1 at 0x1000012f7: file fusion.cpp, line 10.
(gdb) r
Starting program: /Users/avallee/Projects/tmp/fusion
Reading symbols for shared libraries ++............................. done
Breakpoint 1, main (unnamed_arg=0x7fff5fbffb60, unnamed_arg=0x7fff5fbffb60) at fusion.cpp:10
10 std::cout << foo << std::endl;
(gdb) s
boost::fusion::operators::operator<< <boost::fusion::vector<int, char, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> > ([email protected], [email protected]) at out.hpp:38
38 return fusion::out(os, seq);