为什么函数模板不能被部分专门化? [英] Why function template cannot be partially specialized?

查看:249
本文介绍了为什么函数模板不能被部分专门化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

这篇关于为什么函数模板不能被部分专门化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆