Search code examples
c++substr

C++ style and conversion of 0 to size_t(0)


Is using size_t(0) the same as using 0 in the following code?

const string foo = "testing";
const size_t pos = foo.find("i");
string name = foo.substr(size_t(0), pos);

That is, when I put only 0 is it converted to size_t(0)? If so, is one form preferred over the other? My guess is that size_t(0) is the best one to use because it makes the conversion explicit. On the other hand, perhaps some programmers consider size_t(0) verbose? I'm guessing in practice no one cares either way.


Solution

  • That is, when I put only 0 is it converted to size_t(0)?

    The conversion is implicit since substr is:

    basic_string substr( size_type pos = 0,
                         size_type count = npos );
    

    Thus your cast is superfluous.