I want to be able to differentiate array from 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 will call string(const char*)
string bar("bar"); // call string(const char*) instead of the array version
}
The best I have found so far is to use a reference to the pointer instead of a 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 will call string(const char*)
string bar("bar"); // ok, will call the array version
}
it's not exactly the same thing and I want to know if a better way exist
stringoutside of dedicate namespace is calling for trouble.string bar("bar"), I don't pass a pointer but an array, that's were the array to pointer decay kicks in and I want to get the size of the array at compile time if it's available,memcpyis far better thanstrcpy. Otherwise, this class has it's proper namespace ofc, I drop it for clarity.string bar("bar")) . what I want is a better way to discriminate pointer from arrays.initfunction ok? I'm not sure how to do it with constructors.