Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can't GCC resolve this using declaration to the correct type

While coding around in a project I'm working on, I discovered something really odd:

namespace detail {

    struct tuplelike_tag { };
    struct arraylike_tag { };

    template<typename>
    struct call_with_traits;

    template<typename... Ts>
    struct call_with_traits<std::tuple<Ts...>> {
        using tag = tuplelike_tag;

        enum { size = sizeof...(Ts) };
    };

    template<typename T, std::size_t Sz>
    struct call_with_traits<std::array<T, Sz>> {
        using tag = arraylike_tag;

        enum { size = Sz };
    };

    template<typename T, std::size_t Sz>
    struct call_with_traits<T[Sz]> {
        using tag = arraylike_tag;

        enum { size = Sz };
    };

    template<typename F, typename T, int... Is>
    auto call_with(F && f, T && tup, indices<Is...>, tuplelike_tag) -> ResultOf<Unqualified<F>> {
        return (std::forward<F>(f))(std::get<Is>(std::forward<T>(tup))...);
    }

    template<typename F, typename A, int... Is>
    auto call_with(F && f, A && arr, indices<Is...>, arraylike_tag) -> ResultOf<Unqualified<F>> {
        return (std::forward<F>(f))(std::forward<A>(arr)[Is]...);
    }
}

template<typename F, typename Cont>
inline auto call_with(F && f, Cont && cont) -> ResultOf<Unqualified<F>> {
    using unqualified = Unqualified<Cont>;
    using traits  = typename detail::call_with_traits<unqualified>;
    using tag     = typename detail::call_with_traits<unqualified>::tag;
    using no_tag  = typename traits::tag;   // this is what it's all about
    return detail::call_with(std::forward<F>(f), std::forward<Cont>(cont), build_indices<traits::size>(), tag());
}

The odd thing about this code is that tag gets resolved to typename detail::call_with_traits::tag;, but no_tag errors out:

error: no type named ‘tag’ in ‘using traits = struct detail::call_with_traits<typename std::remove_cv<typename std::remove_reference<_To>::type>::type>’

even though it should refer to the same using declaration in the same struct. Is there something I'm missing, or is this some bug in GCC?

A live example can be found here, including the related error message from GCC.

like image 296
Tom Knapen Avatar asked Jun 02 '13 16:06

Tom Knapen


1 Answers

This appears to be a bug in g++ 4.7.2. Here is a minimal example:

template<typename> struct A { using tag = int; };

template<typename T>
inline void f() {
  using AT = A<T>;
  typename A<T>::tag x; // no error
  typename   AT::tag y; // error
}

int main(int argc, char ** argv) {
  f<int>();

  return 0;
}

There are no errors if a typedef is used instead of a using declaration, or if you use A<int> instead of A<T>.

like image 163
Vaughn Cato Avatar answered Nov 14 '22 23:11

Vaughn Cato