gpt4 book ai didi

c++ - 为什么编译器在下面的例子中没有选择我的函数模板重载?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:48 25 4
gpt4 key购买 nike

给定以下函数模板:

#include <vector>
#include <utility>

struct Base { };
struct Derived : Base { };

// #1
template <typename T1, typename T2>
void f(const T1& a, const T2& b)
{
};

// #2
template <typename T1, typename T2>
void f(const std::vector<std::pair<T1, T2> >& v, Base* p)
{
};

为什么下面的代码总是调用重载#1 而不是重载#2?

int main()
{
std::vector<std::pair<int, int> > v;
Derived derived;

f(100, 200); // clearly calls overload #1
f(v, &derived); // always calls overload #1

return 0;
}

鉴于 f 的第二个参数是 Base 的派生类型,我希望编译器会选择重载 #2,因为它比重载 #1 中的泛型。

是否有任何技术可以用来重写这些函数,以便用户可以编写 main 函数中显示的代码(即,利用参数类型的编译器推导)?

最佳答案

您可以这样做:

f(v, static_cast<Base*>(&derived));

或者使用 SFINAE 去除第一个函数作为选择候选:

// Install boost library and add these headers:
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>

// #1 - change it to look like this (note the keyword void changed positions)
template <typename T1, typename T2>
typename boost::disable_if<
typename boost::is_convertible<T2, Base*>, void>::type
f(const T1& a, const T2& b)
{
};

// #2 - this one can stay the same
template <typename T1, typename T2>
void f(const std::vector<std::pair<T1, T2> >& v, Base* p)
{
};

关于c++ - 为什么编译器在下面的例子中没有选择我的函数模板重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567834/

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