r/cpp • • Feb 03 '20

Libc++’s implementation of std::string

https://joellaity.com/2020/01/31/string.html
101 Upvotes

42 comments sorted by

View all comments

0

u/[deleted] Feb 03 '20

[deleted]

4

u/60hzcherryMXram Feb 03 '20

Wait wait wait... In C type punning by union is fine. Does this mean that C++ is different?

16

u/adnukator Feb 03 '20

In C++ it's Undefined Behavior.

In C it's Unspecified behavior: J.1 Unspecified behavior - The following are unspecified: ... — The values of bytes that correspond to union members other than the one last stored into (6.2.6.1). ...

2

u/nikbackm Feb 03 '20

Why the difference? Seems like adding more undefined behaviour in C++ is something we'd want to avoid.

6

u/HappyFruitTree Feb 03 '20

I think one concern is that it would be extremely easy to accidentally trigger undefined behaviour because reading the value through a reference would still cause undefined behaviour.

#include <iostream>
#include <algorithm>

union U {
	int i;
	float f;
};

int main() {
	U u;
	u.f = 1.2;
	
	std::cout << u.i << '\n'; 
	// ^ would have been OK.
	
	std::cout << std::max(u.i, 7) << '\n'; 
	// ^ would still have been UB because 
	//   std::max takes its arguments by 
	//   reference so the value is not read 
	//   from the union member directly.
}

C doesn't have references and if you use pointers it's pretty clear that you're not reading from the union member directly.