在我正在从事的项目中进行编码时,我发现了一些非常奇怪的事情:
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());
}
这段代码的奇怪之处在于tag被解析为typename detail::call_with_traits::tag; ,但no_tag错误:
error: no type named ‘tag’ in ‘using traits = struct detail::call_with_traits<typename std::remove_cv<typename std::remove_reference<_To>::type>::type>’
即使它应该在同一个结构中引用相同的 using 声明。有什么我遗漏的东西,或者这是 GCC 中的一些错误吗?
可以在此处找到一个实时示例,包括来自 GCC 的相关错误消息。