--- Comment #1 from Antony Polukhin <antoshkka at gmail dot com> ---
Another std::variant related example where GCC fails to eliminate
subexpressions and generates 6 times bigger assembly:

using size_t = unsigned long long;

struct A {} a;
static const size_t variant_npos = -1;

struct variant {
    using __index_type = unsigned char; // !!!!  O_O !!!!!!
    __index_type _M_index;

    size_t index() const noexcept {
            return (_M_index == __index_type(variant_npos) ? variant_npos :

template<size_t _Np>
static A* get_if(variant* __ptr) noexcept {
    return (__ptr->index() == _Np ? &a : nullptr);

A* foo(int i, variant& in) {
    if (i==0) return get_if<0>(&in);
    if (i==1) return get_if<1>(&in);
    if (i==2) return get_if<2>(&in);
    if (i==3) return get_if<3>(&in);
    if (i==4) return get_if<4>(&in);
    if (i==5) return get_if<5>(&in);
    if (i==6) return get_if<6>(&in);

    return get_if<7>(&in);

Reply via email to