gpt4 book ai didi

C++ 入门第 5 版。函数模板重载

转载 作者:行者123 更新时间:2023-12-03 10:04:28 24 4
gpt4 key购买 nike

在C++ Primer一书中,有一个关于函数模板重载的例子:

// print any type we don't otherwise handle
template <typename T> string debug_rep(const T &t)
{
cout << "debug_rep(T const&)\n";
ostringstream ret; // see § 8.3 (p. 321)
ret << t; // uses T's output operator to print a representation of t
return ret.str(); // return a copy of the string to which ret is bound
}

// print pointers as their pointer value, followed by the object to which the pointer points
// NB: this function will not work properly with char*; see § 16.3 (p. 698)
template <typename T> string debug_rep(T *p)
{
std::cout << "debug_rep(T*)\n";
ostringstream ret;
ret << "pointer: " << p << '\n'; // print the pointer's own value
if (p)
ret << " " << debug_rep(*p); // print the value to which p points
else
ret << " null pointer"; // or indicate that the p is null
return ret.str(); // return a copy of the string to which ret is bound
}

If we call debug_rep with a pointer:

cout << debug_rep(&s) << endl;

both functions generate viable instantiations:

  • debug_rep(const string* &), which is the instantiation of the first version of debug_rep with T bound to string*

  • debug_rep(string*), which is the instantiation of the second version of debug_rep with T bound to string*

The instantiation of the second version of debug_rep is an exact match for this call.

The instantiation of the first version requires a conversion of the plain pointer to a pointer to const. Normal function matching says we should prefer the second template, and indeed that is the one that is run.


但是如果我将指向字符串的指针声明为 const虽然没有转换,但总是选择第二个版本:
    string const s("hi"); // const
cout << debug_rep(&s) << '\n';
所以我认为这是书中的一个错误,我认为因为版本采用指针是首选,因为我们传递的指针是 const与否( T 将被推导出为 std::string const*std::string* )。
你怎么认为?

最佳答案

书错了。
在第一个示例中,生成的实例化不是 debug_rep(const string* &) ,是debug_rep(string* const&) .即,const限定指针,而不是指向的东西。 (如果本书使用了正确的 const,这会更明显;也就是说,template <typename T> string debug_rep(T const& t) 作为第一个函数模板。)
确实,TT const&与函数模板重载具有相同的优先级;在它们形成重载集的地方,它将是模棱两可的。原因T*被选中 T const&是它更专业;简单地说,任意T*可以传递给采用 T const& 的函数, 而任意 T const&不能传递给采用 T* 的函数.

关于C++ 入门第 5 版。函数模板重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65242593/

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