C++模板专业化与命名空间OP匹配不同的功能

发布于 2025-01-22 22:05:07 字数 805 浏览 2 评论 0原文

我遇到了这样的情况。我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2025-01-29 22:05:07

编译器是否找到与ARGS匹配的最专业的模板功能?

功能模板不能局部专业。最后一个test_2与第一个重载。

template<typename _t> void test_2(_t) { printf("A"); } // overload #1

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"); } // explicit specialization for overload#1
template<typename _t> void test_2(test_thing<_t>) { printf("C"); } // overload #2

给定test_2(_t {}); adl 帮助并查找第二个超载test_2。对于:: test_2(_t {}); ADL不会生效,只能找到第1 test_2(及其专业化)。

Doesn't the compiler find the most specialized template function that matches the args?

Function templates can't be partial specialized. The last test_2 is overloaded with the 1st one.

template<typename _t> void test_2(_t) { printf("A"); } // overload #1

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"); } // explicit specialization for overload#1
template<typename _t> void test_2(test_thing<_t>) { printf("C"); } // overload #2

Given test_2(_t{});, ADL helps and finds the 2nd overloaded test_2. For ::test_2(_t{}); ADL won't take effect, only the 1st test_2 (and its specialization) could be found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文