Why are my two tuples containing strings, created the same way, not equal?

C++Visual C++TuplesString Literals

C++ Problem Overview


I'm compiling the following program using Microsoft Visual C++, as a C++20 program:

#include <iostream>
#include <tuple>

int main()
{
	auto t1 = std::make_tuple("one", "two", "three");
	auto t2 = std::make_tuple("one", "two", "three");
	
	std::cout << "(t1 == t2) is " << std::boolalpha << (t1 == t2) << "\n";
	std::cout << "(t1 != t2) is " << std::boolalpha << (t1 != t2) << "\n";

	return 0;
}

When I run it, I see the following output:

(t1 == t2) is false
(t1 != t2) is true

The tuples are identical, so why does it have wrong comparison results? How do I fix this?

C++ Solutions


Solution 1 - C++

You are comparing pointers to buffers of characters, not strings.

Sometimes the compiler will turn two different "one"s into the same buffer, sometimes it will not.

In your case, it isn't. Probably a debug build.

Add #include <string_view>, then

using namespace std::literals;

auto t1 = std::make_tuple("one"sv, "two"sv, "three"sv);
auto t2 = std::make_tuple("one"sv, "two"sv, "three"sv);

and you'll get what you expect. (In pre-[tag:C++17] compilers, use <string> and ""s instead of <string_view> and ""sv).

Solution 2 - C++

What is the type of "one"? This is not a string, but rather a string literal.

Your problem basically boils down to this code:

char const* a = "one";
char const* b = "one";

std::cout << "(a == b) is " << std::boolalpha << (a == b) << "\n";
std::cout << "(a != b) is " << std::boolalpha << (a != b) << "\n";

Which will most likely output the same result.

This is because a string literal will decay into a char const*. Comparing two pointer compares their location in memory. Now this is a matter of whether your compiler is folding string literals into one. If the string literals are folded, then they are gonna be equal, if they are not, they are not gonna be equal. This can vary with different optimization levels.

How can you fix your comparison then?

Preferably use std::string_view as you don't seem to need to own or change their content:

using namespace std::literals;

// ... 

auto t1 = std::make_tuple("one"sv, "two"sv, "three"sv);
auto t2 = std::make_tuple("one"sv, "two"sv, "three"sv);

The std::string_view class is a thin wrapper around a pointer and a size, and define a comparison operator that check for value equality.

Solution 3 - C++

The problem is unrelated to C++20, but comes from how string literals are implemented. The answer is for example here:

https://stackoverflow.com/questions/52814457/why-do-only-some-compilers-use-the-same-address-for-identical-string-literals

In short, your program falls into the category of "undefined unspecified behavior", as it assumes that identical C-style string literals have identical addresses. This is because expressions like "a" == "a" compare addresses, not the content. Your code could be made safe and predictable if you used std::string literals, like "one"s, "one"sv etc., see https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

Solution 4 - C++

auto is not always your friend. I would argue the proper way to get reliably the “right” behaviour without boilerplate is to explicitly use a type that you know has value-equality. Then you can also omit the make_tuple and simply use the initialiser-list constructor:

#include <string>
#include <tuple>
#include <iostream>

typedef std::tuple<std::string, std::string, std::string> StrTriple;

int main() {
  
  StrTriple t1{"one", "two", "three"};
  StrTriple t2{"one", "two", "three"};

  std::cout << "(t1 == t2) is " << std::boolalpha << (t1 == t2) << "\n";
  std::cout << "(t1 != t2) is " << std::boolalpha << (t1 != t2) << "\n";

    return 0;
}

No doubt some would argue that the memory management of std::string incurs unnecessary overhead. string_view may be preferrable, however chances are in a real-world application the strings will need to be dynamically allocated anyway somewhere.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAeroSunView Question on Stackoverflow
Solution 1 - C++Yakk - Adam NevraumontView Answer on Stackoverflow
Solution 2 - C++Guillaume RacicotView Answer on Stackoverflow
Solution 3 - C++zkozaView Answer on Stackoverflow
Solution 4 - C++leftaroundaboutView Answer on Stackoverflow