से अधिक प्राथमिकता क्यों है, मैं निम्नलिखित कोड में एक व्यवहार देख रहा हूं जिसे मैं समझ नहीं पा रहा हूं। बात यह है कि अगर मैं दोनों में से किसी की तरह operator()
के दूसरे अधिभार की घोषणा के बाद:boost :: संस्करण - एक टेम्पलेट पैरामीटर को एक स्थिर स्ट्रिंग पैरामीटर
bool operator()(T other) const
bool operator()(const T &other) const
कार्यक्रम का उत्पादन होता है:
स्ट्रिंग
लेकिन निम्नलिखित अगर मैं का उपयोग घोषणा:
bool operator()(T &other) const
आउटपुट होगा:
अन्य प्रकार
किसी कृपया समझा क्यों operator()(const string &other)
उत्तरार्द्ध मामले में बुलाया जा रहा है ना?
#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"
using namespace std;
using namespace boost;
typedef variant<string, int> MyVariant;
class StartsWith
: public boost::static_visitor<bool>
{
public:
string mPrefix;
bool operator()(const string &other) const
{
cout << "string" << endl;
return other.compare(0, mPrefix.length(), mPrefix) == 0;
}
template<typename T>
bool operator()(T &other) const
{
cout << "other type" << endl;
return false;
}
StartsWith(string const& prefix):mPrefix(prefix){}
};
int main(int argc, char **argv)
{
MyVariant v(string("123456"));
apply_visitor(StartsWith("123"), v);
return 0;
}
एक प्रकार के सनकी के रूप में, मैं कहूंगा कि ** तीसरा ** समाधान ** सर्वोत्तम ** है, जैसा कि: ** कॉन्स्ट-सही ** है। यदि आप इसे संशोधित नहीं कर रहे हैं तो गैर-कॉन्स्ट संदर्भ द्वारा पैरामीटर लेने का कोई कारण नहीं है। –
@MatthieuM। इस मामले में आप सही हैं।मैंने 'टी एंड एक्स 'का उल्लेख नहीं किया - शायद यह सबसे अच्छा होगा - लेकिन' एस एंड टी 'और' कॉन्स टी 'के बीच अंतर पर चर्चा करने वाले कई एसओ पोस्ट हैं इसलिए मैं यहां इस अनावश्यक मिश्रण को नहीं बनाना चाहता था ... – PiotrNycz