I have created the following NullTerminatedStringView class:
Why can't my NullTerminatedStringView be created from std::string without writing explicit constructor for it? As I understand, it should be able to do it automatically.
#include <string>
#include <string_view>
class NullTerminatedStringView
{
private:
std::string_view stringView;
public:
NullTerminatedStringView( std::string_view const stringView )
: stringView( stringView ) { }
operator std::string_view () const
{
return stringView;
}
const char * cStr() const
{
return stringView.data();
}
size_t size() const
{
return stringView.size();
}
};
void f( NullTerminatedStringView const path ) { }
int main()
{
std::string ss = "string";
f( ss ); // no matching function for call to 'f'
}
You can test it here: https://godbolt.org/z/cvjbaG