I've discovered a bug in Xcode 16.0/16.1 where using std::variant in containers across compilation units triggers UBSan errors. This is a regression as it works correctly in Xcode 15.4.
Here's a minimal reproduction case:
[base.h]
#pragma once
#include <string>
#include <variant>
#include <forward_list>
class Item {
public:
std::variant<std::monostate, std::string> value;
};
typedef std::forward_list<Item> ItemList;
class Test {
public:
void addItem(const Item& item);
ItemList items;
};
[base.cpp]
#include "base.h"
void Test::addItem(const Item& item) {
items.push_front(item);
}
[main.cpp]
#include "base.h"
int main() {
Test t;
Item item;
t.addItem(item);
return 0;
}
To reproduce:
Compile with UBSan enabled (-fsanitize=undefined)
Occurs on both arm64 and x86_64
Occurs in both Xcode 16.0 and 16.1
Works correctly in Xcode 15.4
I've filed a Feedback Assistant report: FB15710420
Workaround:
The issue can be avoided by implementing the addItem method in the header file instead of a separate compilation unit.
Has anyone else encountered this issue? Are there other workarounds besides moving the implementation to the header?