c++函数模板(重载和特化)

问题描述 投票:0回答:0

我不能理解一件事。我有一个 foo() 函数

template\<typename T\>
void foo(T)
{
 std::cout\<\<1;
}

template\<\>
void foo(int\*)
{
 std::cout\<\<2;
}

template\<typename T\>
void foo(T\*)
{
 std::cout\<\<3;
}

int main()
{
 int i;
 foo(&i);
}

当我运行程序时,输出是 3。如果我更改最后两个函数,输出将是 2。为什么?我认为在这两种情况下都是 2。请帮忙

templates
© www.soinside.com 2019 - 2024. All rights reserved.