2012-05-07 21 views
16

मैं एक कार्यक्रम बूस्ट कार्यक्रम विकल्प पुस्तकालय का उपयोग करता है लिख रहा हूँ और मैं निम्न सिंटैक्स कि मुझे अड्डा है के बाद से मैं इसे देखा देखा:बूस्ट कार्यक्रम विकल्प विकल्प जोड़े सिंटेक्स

desc.add_options() 
     ("help","produce help message") 
     (/* other flag, value, description pairs here */) 
; 

मुझे लगता है कि शीर्ष लेख, ऑपरेटर में() ओवरराइड है, लेकिन मुझे यकीन नहीं है कि यह सिंटैक्टिक रूप से सही कैसे हो सकता है।

दूसरा, क्या इस वाक्यविन्यास का कोई फायदा है, केवल add_options() को कई बार कॉल करने की तुलना में (इस तथ्य को दिखाने के अलावा कि आप इस तरह वाक्यविन्यास में हेरफेर कर सकते हैं)?

+5

बूस्ट लेखकों को दिखाना पसंद है ... –

+0

"प्रेतवाधित" एक अच्छा विवरण है ... मैं इसे औपचारिक रूप से प्राप्त करता हूं, लेकिन "अजीब" लगता है ... – thomastiger

उत्तर

16

add_options सदस्य फ़ंक्शन options_description_easy_init प्रकार का ऑब्जेक्ट देता है। बाद में एक संदर्भ वापस करने के लिए operator() अधिभारित है। यह आपको स्निपेट में दिखाए गए कॉल को चेन करने की अनुमति देता है।

कॉल चेनिंग और add_options कई बार फोन करने के बीच अंतर यह है कि पूर्व के मामले options_description_easy_init का एक उदाहरण बन जाता है और हर बार में आप उस पर operator() आह्वान, यह मालिक (options_description) के लिए विकल्प जोड़ता है। यदि आप add_options को कई बार कॉल करना चाहते थे तो प्रत्येक कॉल options_description_easy_init का एक नया उदाहरण बनायेगी।

+1

+1 के लिए +1, लेकिन यह अभी भी begs प्रश्न: क्यों? – paulrehkugler

+0

@ पोलरेहकुगलर निकोल का [उत्तर] (http://stackoverflow.com/a/10487655/241631) बताता है कि क्यों – Praetorian

12

लाभ प्रश्न व्यक्तिपरक है, लेकिन इस मामले में यह संक्षिप्त है।

मेरे घर परियोजनाओं में से एक से तुलना करें:

("help,h", "Generate this help message") 
("output-file,o", po::value<std::string>(), "Output filename. Required.") 
("tangent,t", "Generate/load tangent-space basis.") 
("collada-output,c", "Write a Collada file, rather than our mesh XML format.") 
("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing.") 
("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent.") 
("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file.") 
("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n" 
     "Name # # # #\n" 
     "\n" 
     "Each # is an attribute index to use for this VAO.\n" 
     "Each VAO name must be unique; you cannot use the same VAO in the same place.") 

यह करने के लिए:

visible.add_options()("help,h", "Generate this help message") 
visible.add_options()("output-file,o", po::value<std::string>(), "Output filename. Required.") 
visible.add_options()("tangent,t", "Generate/load tangent-space basis."); 
visible.add_options()("collada-output,c", "Write a Collada file, rather than our mesh XML format."); 
visible.add_options()("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing."); 
visible.add_options()("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent."); 
visible.add_options()("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file."); 
visible.add_options()("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n" 
     "Name # # # #\n" 
     "\n" 
     "Each # is an attribute index to use for this VAO.\n" 
     "Each VAO name must be unique; you cannot use the same VAO in the same place."); 

लाइन की लंबाई मायने रखती है। और सब कुछ के सामने visible.add_options() नहीं होने के कारण इसे पढ़ने में आसान बनाता है।

+0

मुझे संदेह था कि यह एकमात्र फायदा था। मेरी राय में, यह सुंदर कोड बनाने के लिए एक और अधिक काम है। – paulrehkugler

+0

दोनों उदाहरण बहुत भयानक हैं। क्या आप 'options_description_easy_init o = visible.add_options()' को परिभाषित कर सकते हैं और बस 'ओ ("सहायता", "यह सहायता संदेश जेनरेट करें") को कॉल करें; '? – Vortico

+0

@ वोर्टिको: मुझे पहले मामले के बारे में विशेष रूप से अप्रिय नहीं दिखता है। दरअसल, मैं नहीं देखता कि आपके सुझाव के पहले लाभ पर क्या लाभ है, भले ही यह काम करता हो। मुझे नहीं पता कि यह क्या होगा; आपको बूस्ट के दस्तावेज को देखने के लिए जांचना होगा। लेकिन आम तौर पर, मध्यवर्ती को इस तरह की संरचनाओं से रखने का अच्छा विचार नहीं है। –