- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下代码在 Visual Studio 下编译得很好,但 gcc 4.6.2 或 4.7 都不能处理它。它似乎是有效的,但 gcc 似乎无法解决 const 和非 const 参数之间的区别。这可能是编译器错误吗?
struct CReadType{};
struct CWriteType{};
template<typename ReadWriteType, typename T>
struct AddPkgrConstByType {};
template<typename T>
struct AddPkgrConstByType<CReadType, T> {
typedef T type;
};
template<typename T>
struct AddPkgrConstByType<CReadType, const T> {
typedef T type;
};
template<typename T>
struct AddPkgrConstByType<CWriteType, T> {
typedef T const type;
};
template<typename Packager, typename T>
struct AddPkgrConst : public AddPkgrConstByType<typename Packager::CReadWriteType, T> {
};
template<typename Packager, typename T>
inline bool Package( Packager* ppkgr, T* pt )
{
return true;
}
template<typename Packager>
inline bool Package( Packager* ppkgr, typename AddPkgrConst<Packager,bool>::type* pb)
{
return false;
}
struct ReadPackager {
typedef CReadType CReadWriteType;
};
struct WritePackager {
typedef CWriteType CReadWriteType;
};
int main(int argc, char* argv[])
{
ReadPackager rp;
WritePackager wp;
bool b = true;
const bool cb = false;
Package( &rp, &b );
}
编译器调用:
g++ -fPIC -O -std=c++0x -Wno-deprecated -D_REENTRANT
g++-D__STDC_LIMIT_MACROS -c test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:58:22: error: call of overloaded ‘Package(ReadPackager*, bool*)’ is ambiguous
test.cpp:58:22: note: candidates are:
test.cpp:31:6: note: bool Package(Packager*, T*) [with Packager = ReadPackager, T = bool]
test.cpp:38:6: note: bool Package(Packager*, typename AddPkgrConst<Packager, bool>::type*) [with Packager = ReadPackager, typename AddPkgrConst<Packager, bool>::type = bool]
最佳答案
这对我来说像是一个编译器错误。这里涉及的问题是重载决议和模板函数的偏序。由于两个模板函数都可以匹配参数列表 (ReadPackager*, bool)
, 应使用模板函数的偏序来选择更专业的模板函数。
简而言之,如果模板函数的参数始终可以用作另一个模板函数的参数,则该模板函数至少与另一个模板函数一样专用。
很明显,任何两个指针参数都匹配第一个 Package() 函数,但例如 Package(ReadPackager*, const int*) 不能匹配第二个。这似乎暗示第二个 Package 函数更专业,应该可以解决任何歧义。
但是,由于编译器之间存在分歧,因此简化解释可能会忽略一些细微之处。因此,我将遵循从标准中确定函数模板偏序的过程来辨别正确的行为。
首先,将功能标记为P1和P2,以便于引用。
P1:
template<typename Packager, typename T>
bool Package( Packager* ppkgr, T* pt );
P2:
template<typename Packager>
bool Package( Packager* ppkgr, typename AddPkgrConst<Packager,bool>::type* pb);
标准说对于每个模板函数(T1),我们必须为它的每个模板参数泛型唯一类型,使用这些类型来确定函数调用参数类型,然后使用这些类型推导出其他类型模板 (T2)。如果成功,第一个模板 (T1) 至少与第二个 (T2) 一样专业。先P2->P1
U
对于模板参数 Packager
P2.P1
执行类型推导的参数列表。 Packager
推导为 U
和 T
推导为 AddPkgrConst<Packager,U>::type
.这成功了,P1 被判断为不比 P2 更特化。
现在 P1->P2:
U1
和 U2
用于模板参数 Packager
和 T
P1 的参数列表 (U1*, U2*)。P2
执行类型推导的参数列表。 Packager
推导为 U1。AddPkgrConst<U1,bool>::type
计算结果为 bool
.这与第二个参数 U2
不匹配.如果我们继续执行第 4 步,此过程将失败。但是,我怀疑拒绝此代码的编译器不会执行第 4 步,因此仅仅因为类型推导成功就认为 P2 不比 P1 更专业。这似乎违反直觉,因为 P1 显然接受 P2 所做的任何输入,反之亦然。这部分标准有些绕,所以不清楚是否需要做最后的比较。
让我们尝试通过应用 §14.8.2.5 第 1 段从类型推导出模板参数来解决这个问题
Template arguments can be deduced in several different contexts, but in each case a type that is specified in terms of template parameters (call it P) is compared with an actual type (call it A), and an attempt is made to find template argument values (a type for a type parameter, a value for a non-type parameter, or a template for a template parameter) that will make P, after substitution of the deduced values (call it the deduced A), compatible with A.
在我们的类型推导中,推导的 A 是 AddPkgrConst<U1,bool>::type
= bool
.这与原始 A 不兼容,后者是唯一类型 U2
.这似乎支持偏序解决歧义的立场。
关于c++ - gcc4 模板错误或更可能是 id10t 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9037940/
出现在 python 2.7.8 中。 3.4.1 不会发生这种情况。 示例: >>> id(id) 140117478913736 >>> id(id) 140117478913736 >>> id
好吧,我对动态创建的控件的 ID 很困惑。 Public Class TestClass Inherits Panel Implements INamingContainer
我收到下面的错误,说有堆栈溢出。发生这种情况是因为带有 IN (id, id, id...id) 的 SQL 语句有大量参数。有没有什么办法解决这一问题?这是在我使用 Eclipse 的本地环境中发生
为什么 CPython(不知道其他 Python 实现)有以下行为? tuple1 = () tuple2 = ()
为什么 CPython(对其他 Python 实现一无所知)有以下行为? tuple1 = () tuple2 = ()
非常简单的问题:当我有一个持久对象时,它通常有一个名为 ID 的属性(对于抽象类)。 那么..命名约定是ID还是Id? 例如。 public int ID { get; set; } 或 public
知道为什么我会收到此错误,我已经尝试了所有命名约定(小写/大写) 我正在使用 Vaadin,这是我的代码片段: public class Usercontainer extends BeanI
为什么 CPython(不知道其他 Python 实现)有以下行为? tuple1 = () tuple2 = ()
我需要改变表的所有主键 UPDATE TODO SET id = id + 1 但我做不到(Demo 来自 Ahmad Al-Mutawa 的回答)描述了原因。主键不能这样改。 我也不能根据这是 sq
我正在尝试列出与用户相关的讨论列表。 想象一下,如果你愿意的话: posts -------------------------------------------------------------
我有一个表,其中包含一些具有自己的 ID 和共享 SKU key 的文章。我尝试使用左连接进行查询,并使用组结果获取从查询返回的所有 id。 我的数据结构是这样的: id - name -
在下表People中: id name 1 James 2 Yun 3 Ethan 如果我想找到最大 ID,我可以运行此查询 select max(id) id from People; 结果是
我正在产品页面上创建评论模块,其中显示垃圾评论选项,并显示 onclick 显示和隐藏弹出窗口。现在它在单个评论中工作正常但是当评论是两个时它同时打开两个因为类是相同的。现在这就是为什么我想要获取父
根据 REST 哲学,PUT操作应该(取自维基百科): PUT http://example.com/resources/142 Update the address member of the co
我想知道如何在使用 PHP 或 JavaScript 进行身份验证后从 Google Analytics 获取 Property Id、View Id 和 Account Id?因为我希望能够将它们存
我想使用所选按钮的 ID 进行删除。但我不知道如何从中获取/获取 id。我尝试了 this.id 但不起作用。 这是我创建按钮的地方: var deleteEmployer= document.cre
我有一个具有以下结构的表“表” ID LinkedWith 12 13 13 12 14 13 15 14 16
请不要在未阅读问题的情况下将问题标记为重复。我确实发布了一个类似的问题,但 STACKOVERFLOW 社区成员要求我单独重新发布修改后的问题,因为考虑到一个小而微妙的修改,解决方案要复杂得多。 假设
在 Android Studio 中,我创建了一个 Person.java 类。我使用Generate 创建了getter 和setter 以及构造函数。 这是我的 Person.java 类: pu
如何在 jQuery 中制作这样的东西: //这是显示的主体 ID //当我悬停 #hover-id 时,我希望 #principal-id 消失并更改 。但是当我将光标放在 #this-id 上时
我是一名优秀的程序员,十分优秀!