如何对两个参数使用部分特化

发布于 2025-01-05 07:02:40 字数 417 浏览 2 评论 0原文

我可以发誓语法是正确的。我尝试了一下,将 class 更改为 typename。还是不行。

我该如何编写这个以便第二个功能模板启动?

#include <iostream>

template<typename T, typename TT> void fn(T t, TT tt) { std::cout<<"general"<<std::endl; }
template<> void fn<T, bool>(T t, bool tt) { std::cout<<"bool"<<std::endl; }
int main(){
    fn("", "");
    fn("", true);
}

I could swear the syntax is correct. I played around and changed class into typename. Still no go.

How do i write this so the 2nd function template kicks in?

#include <iostream>

template<typename T, typename TT> void fn(T t, TT tt) { std::cout<<"general"<<std::endl; }
template<> void fn<T, bool>(T t, bool tt) { std::cout<<"bool"<<std::endl; }
int main(){
    fn("", "");
    fn("", true);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遇到 2025-01-12 07:02:40

函数模板没有部分特化。只需使用重载即可:

template<typename T> void fn(T t, bool tt) { std::cout<<"bool"<<std::endl; }

There are no partial specializations of function templates. Just use overloading instead:

template<typename T> void fn(T t, bool tt) { std::cout<<"bool"<<std::endl; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文