gpt4 book ai didi

c++ - gcc4 模板错误或更可能是 id10t 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:43 27 4
gpt4 key购买 nike

以下代码在 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

  1. 合成唯一类型 U对于模板参数 Packager P2.
  2. P1 执行类型推导的参数列表。 Packager推导为 UT推导为 AddPkgrConst<Packager,U>::type .

这成功了,P1 被判断为不比 P2 更特化。

现在 P1->P2:

  1. 合成唯一类型 U1U2用于模板参数 PackagerT P1 的参数列表 (U1*, U2*)。
  2. P2 执行类型推导的参数列表。 Packager推导为 U1。
  3. 不对第二个参数执行推导,因为作为依赖类型,它被视为非推导上下文。
  4. 因此第二个参数是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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com