Am 26.01.2017 10:47, schrieb Richard Hodges:
I don't see any particular reason why you can't provide all of them, though (even the implicit conversion), as different things are going to feel more natural to different people, particularly in different contexts. There's a problem with implicit conversion. imagine the overloads function: void foo(std::string const& s); [...] foo(join(...)); [...] void foo(std::string_view s); void foo(const char* s); Now the above code will not compile, because there are 3 ambiguous overloads.
But that is only an issue for users, who have relied on the implicit conversion upfront. You can always use an implicit conversion explicitly as well, of course, and implicit conversion will be just one of multiple options, if we add it.
You don't know why the user added the overloads for foo, perhaps she suddenly had to adapt foo so that it also works with C code which uses a lot of const char*. As a designer, you have no control over other peoples' interfaces. You argued about the principle of least surprise. Let's say the user started to use concat in her code with a function foo(std::string const & s). Then she decided to add to the overload foo(const char* s). All of a sudden her code does not compile anymore. This should not happen. As a user, she will be very surprised at that moment. That's why it is a bad idea to have implicit conversions. Why do you think the standards committee added explicit operator <type>() to the language? They don't do these language changes for fun. Also, if you won't take it from us, please go and take the wisdom from Herb Sutter http://www.gotw.ca/gotw/019.htm "It's almost always a good idea to avoid writing automatic conversions, either as conversion operators or as single-argument non-explicit constructors."
The call is then auto s = static_caststd::string(concat(…));
That is the worst we had up to now. Even an implicit conversion is better, which is not at all my favourite as well.
As Olaf pointed out, it is sufficient to do auto s = std::string(concat(…)); you don't need the static_cast. Nevertheless, casts are the official way of doing type conversions, whether you like the syntax or not. I think Stroustrup intentionally made them ugly, because he wanted type conversions to be the exception in his statically typed language. Hans