Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::max not working for string literals?

I am trying to find the maximum of two strings and it is giving me the correct answer in the first case (when passing std::string variables) but giving an error in the second case (when passing direct strings).

#include<bits/stdc++.h>
using namespace std;

int main()
{
    // Case 1
    string str1 = "abc", str2 = "abcd";
    cout << max(str1, str2) << endl;

    // Case 2
    cout << max("abc", "abcd") << endl;
}
like image 736
Yuvraj Parashar Avatar asked Sep 10 '21 06:09

Yuvraj Parashar


People also ask

Is std::string literal?

std::string literals are Standard Library implementations of user-defined literals (see below) that are represented as "xyz"s (with a s suffix). This kind of string literal produces a temporary object of type std::string , std::wstring , std::u32string , or std::u16string , depending on the prefix that is specified.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

What is the maximum length of a string in characters in C++?

While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.

Does std::string store size?

std::string actually maintains the size as one of its data member.


Video Answer


1 Answers

In your second case,

std::cout << std::max("abc", "abcd") << std::endl;

they are string literals, in which "abc" has type char const [4] and "abcd" has type char const [5].

Therefore, in the function call std::max("abc", "abcd"), the std::max has to deduce

auto max(char const (&a)[4], char const (&b)[5]) {
    return a < b ? b : a;
}

This is not possible as the std::max has no function template overload, which takes different types as template arguments. Hence, the error!


Warning!

If you explicitly mention the template type const char* in std::max, this could have been compiled. This is because, for "abc" and "abcd" the type can be also be const char*s due to array to pointer decay in C++.

 std::cout << std::max<const char*>("abc", "abcd" ) << '\n';  // compiles
                      ^^^^^^^^^^^^^

In addition, the std::initializer_list overload of the std::max, in turn will deduce the above also const char* as template type:

std::cout << std::max({ "abc", "abcd" }) << '\n';   // compiles

However, you should not be doing it !

As @AlanBirtles pointed out, this can cause the undefined behavior, due to the fact that the std::max will compare the pointers of the two different arrays. The result can not be relayed and should be doing the above. Use the std::string for comparison as in your first case. Using string literals (since C++14), you can do a minimal change, and make the second case, same as first one:

#include <string>
using namespace std::string_literals;

std::cout << std::max("abc"s, "abcd"s) << '\n';

As a side note, see the followings:

  • Why should I not #include <bits/stdc++.h>?
  • Why is "using namespace std;" considered bad practice?
like image 90
JeJo Avatar answered Nov 14 '22 21:11

JeJo