I feel like I'm missing something obvious about int type promotion when assigning to a variant.
On gcc version 9.3.0 (Ubuntu 9.3.0-11ubuntu0~18.04.1), compiling with -std=c++17, the following code fails to compile:
#include <variant>
#include <iostream>
int main()
{
std::variant<long int, bool> v; // works fine if "long" is omitted
long int sanity = 1; // verify that we can assign 1 to a long int; works fine
std::cout << sizeof(sanity) << "\n";
v = 1; // compiler error here: why doesn't this assign to the long int variant of v?
return 0;
}
Error message:
error: no match for ‘operator=’ (operand types are ‘std::variant<long int, bool>’ and ‘int’)
Is there any magic to making this work as intended, without requiring explicit casts in the assignment? Thanks!