gpt4 book ai didi

指针函数/const 指针函数的 C++ 模板部分特化没有区别吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:23 24 4
gpt4 key购买 nike

让我们考虑以下代码:

template <typename T>
class Foo
{};

template <typename T, typename U>
class Foo<T(*)(U* const)>
{};

template <typename T, typename U>
class Foo<T(*)(U*)>
{};

当我尝试编译它时 (ideone)它没有告诉我这两个模板特化是相同的。这是令人惊讶的,因为通常 U*U* const 是不同的东西(第二个是 const 指针)。这里有什么问题吗?

最佳答案

在确定函数的类型(通俗地称为其签名)时,将删除顶级 cv 限定符。

§8.3.5/5 ... The type of a function is determined using the following rules. ... After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. ...

对于使 this 无效的函数指针或模板参数没有特殊规则。

关于指针函数/const 指针函数的 C++ 模板部分特化没有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266308/

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