Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access by type in std::tuple with duplicated types should produce compilation error

Tags:

c++

std

c++14

According to the standard (or at least to cppreference) the std::get for std::tuple shall:

5-8) Extracts the element of the tuple t whose type is T. Fails to compile unless the tuple has exactly one element of that type.

So I interpret that sentence such that this code does not compile:

std::tuple<int, int> my_record;
std::get<int>(my_record) = 10;

Because two identical types exist and I try to access the tuple by type. However, both GCC an Clang correctly compile this code and produce the effect of modifying the first element.

Why? Am I misinterpreting the sentence on the reference? Is the reference wrong? Do GCC and Clang not respect the standard?

like image 643
ocirocir Avatar asked Jul 12 '21 07:07

ocirocir


1 Answers

Looks like a GCC 11 bug, consider filing it. Here's the revelant part of the standard.

You see it in Clang because on gcc.godbolt.org it uses GCC's standard library by default. If you add -stdlib=libc++ to use it's own standard library, it refuses to compile it.

like image 191
HolyBlackCat Avatar answered Oct 20 '22 06:10

HolyBlackCat