c++ - array decay to pointer and overload resolution -
i want able differentiate array pointers in overload resolution :
class string { public: string(const char* c_str); template<int n> string(const char (&str) [n]); }; int main() { const char* c_str = "foo"; string foo(c_str); // ok call string(const char*) string bar("bar"); // call string(const char*) instead of array version } the best have found far use reference pointer instead of pointer :
class string { public: string(const char*& c_str); template<int n> string(const char (&str) [n]); }; int main() { const char* c_str = "foo"; string foo(c_str); // ok call string(const char*) string bar("bar"); // ok, call array version } it's not same thing , want know if better way exist
you need make first overload poorer choice when both viable. tie on conversion ranking (both "exact match"), , tie broken because non-templates preferred.
this ought make conversion ranking worse:
struct stg { struct cvt { const char* p; cvt(const char* p_p) : p(p_p) {} }; // matches const char*, disfavored in overload ranking stg(cvt c_str); // use c_str.p inside :( or add implicit conversion template<int n> stg(const char (&str) [n]); };
Comments
Post a Comment