为什么 GCC 不能使用声明为正确的类型解决此问题
why can't GCC resolve this using declaration to the correct type
当我在一个项目中编码时,我发现了一些非常奇怪的事情:
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());
}
这段代码的奇怪之处在于标签被解析为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中的一些bug ?
可以在这里找到一个实例,包括来自GCC的相关错误消息。
这似乎是g++ 4.7.2中的一个bug。下面是一个简单的例子:
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;
}
如果使用typedef
而不是using
声明,或者使用A<int>
而不是A<T>
,则不会出现错误
相关文章:
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- Eclipse/CDT_C++给出"语义错误_"类型XXX无法解决"。项目运行
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- "class":非法使用此类型作为表达式 如何解决?
- 表达式必须具有类类型。不知道怎么解决
- 解决由于在哈希函数中使用了不完整的类型而导致的编译失败
- C++ 解决方法:"从类型"B*"的表达式初始化类型"C*&"的引用无效"
- 对前向声明类型进行unique_ptr的解决方法
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 类型长的长度和未解决的超载函数类型的类型的操作数与二进制运算符
- 清.如何生成具有不同库类型的Visual Studo解决方案?
- 错误:"某个类"未命名类型。如何解决这个问题?
- dlsym() 解决方法返回类型
- 在 C++14 中,是否有一种优雅的解决方案可以在可变参数模板中选择可调用和不可调用的类型
- 我将类型库(.tlb)导入到Delphi中,但函数参数似乎不正确.我应该如何解决它
- 在这种情况下,有没有办法用单个解决方案替换两个仅在类型上不同的相似函数?
- 任何解决此类型扣除的方法
- 我的头文件中的错误 - 错误:"QSMatrix"没有命名类型,如何解决此问题?
- 为什么 GCC 不能使用声明为正确的类型解决此问题
- 用于"types"点的强类型解决方案