C++模板专业化与命名空间OP匹配不同的功能
我遇到了这样的情况。我在Windows 10上使用G ++。
#include <stdio.h>
template<typename _t>
struct test_thing {};
template<typename _t> void test_2(_t) { printf("A"); }
template<typename _t>
void test()
{
//test_2(_t{}); // prints: ABC
::test_2(_t{}); // prints: ABA <-- namespace op, searching different?
}
template<> void test_2(double) { printf("B"); }
template<typename _t> void test_2(test_thing<_t>) { printf("C"); }
int main()
{
test<int>();
test<double>();
test<test_thing<int>>();
return 0;
}
我的问题是,命名空间OP的原因/如何更改编译器搜索要调用的函数的方式。编译器是否找到与ARGS匹配的最专业的模板功能?如果test_2(test_thing&lt; _t&gt;)
在上面定义test
它可以找到它,但仅使用::
,而不是下面。
I have run into a situation that looks like this. I am using g++ on Windows 10.
#include <stdio.h>
template<typename _t>
struct test_thing {};
template<typename _t> void test_2(_t) { printf("A"); }
template<typename _t>
void test()
{
//test_2(_t{}); // prints: ABC
::test_2(_t{}); // prints: ABA <-- namespace op, searching different?
}
template<> void test_2(double) { printf("B"); }
template<typename _t> void test_2(test_thing<_t>) { printf("C"); }
int main()
{
test<int>();
test<double>();
test<test_thing<int>>();
return 0;
}
My question is why/how is the namespace op changing how the compiler is searching for the function to call. Doesn't the compiler find the most specialized template function that matches the args? If test_2(test_thing<_t>)
is defined above test
it finds it, but only with the ::
, not below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
功能模板不能局部专业。最后一个
test_2
与第一个重载。给定
test_2(_t {});
, adl 帮助并查找第二个超载test_2
。对于:: test_2(_t {});
ADL不会生效,只能找到第1test_2
(及其专业化)。Function templates can't be partial specialized. The last
test_2
is overloaded with the 1st one.Given
test_2(_t{});
, ADL helps and finds the 2nd overloadedtest_2
. For::test_2(_t{});
ADL won't take effect, only the 1sttest_2
(and its specialization) could be found.