为什么函数模板不能被部分专门化?

编程入门 行业动态 更新时间:2024-10-27 21:23:37
本文介绍了为什么函数模板不能被部分专门化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道语言规范禁止功能模板的部分专业化。

I know the language specification forbids partial specialization of function template.

我想知道为什么它禁止的理由?

I would like to know the rationale why it forbids it? Are they not useful?

template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed!

推荐答案

AFAIK在C ++ 0x 。

我想这只是一个疏忽(考虑到你可以总是获得部分专业化效果与更详细的代码,通过将函数作为 static 一个类的成员)。

I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class).

您可以查找相关的DR(缺陷报告) 。

You might look up the relevant DR (Defect Report), if there is one.

EDIT :检查这一点,我发现其他人也相信,但没有人能够在标准草案。 此SO线程似乎表明不支持功能模板的部分专门化在C ++ 0x 中。

EDIT: checking this, I find that others have also believed that, but no-one is able to find any such support in the draft standard. This SO thread seems to indicate that partial specialization of function templates is not supported in C++0x.

编辑2 :只是一个例子, code> static 类的成员:

EDIT 2: just an example of what I meant by "placing the function as a static member of a class":

#include <iostream> using namespace std; // template<typename T, typename U> void f() {} //allowed! // template<> void f<int, char>() {} //allowed! // template<typename T> void f<char, T>() {} //not allowed! // template<typename T> void f<T, int>() {} //not allowed! void say( char const s[] ) { std::cout << s << std::endl; } namespace detail { template< class T, class U > struct F { static void impl() { say( "1. primary template" ); } }; template<> struct F<int, char> { static void impl() { say( "2. <int, char> explicit specialization" ); } }; template< class T > struct F< char, T > { static void impl() { say( "3. <char, T> partial specialization" ); } }; template< class T > struct F< T, int > { static void impl() { say( "4. <T, int> partial specialization" ); } }; } // namespace detail template< class T, class U > void f() { detail::F<T, U>::impl(); } int main() { f<char const*, double>(); // 1 f<int, char>(); // 2 f<char, double>(); // 3 f<double, int>(); // 4 }

更多推荐

为什么函数模板不能被部分专门化?

本文发布于:2023-11-25 16:39:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!