【问题标题】:Why function template cannot be partially specialized?为什么函数模板不能部分特化?
【发布时间】:2011-07-03 08:35:39
【问题描述】:

我知道语言规范禁止函数模板的部分特化。

我想知道为什么它禁止它的理由?它们没有用吗?

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!

【问题讨论】:

  • 对于template&lt;typename T, typename U&gt; void f(T t, U u) {} 也允许template&lt;&gt; void f(int t, char u) {}
  • 我觉得有趣的是,当问题不是“我怎样才能实现类似的目标”而是“这种行为背后的基本原理是什么”时,人们不断提供解决方法......我自己也不知道原因这种选择,但我认为委员会一定有理由禁止功能模板部分专业化。到目前为止,“最接近”的解释是 Georgy 发布的链接,该链接仅指出存在重载时函数模板部分特化的潜在“风险”。但是,我认为这不是禁止此功能的理由,所以我认为还有更多……

标签: c++ language-design template-specialization partial-specialization function-templates


【解决方案1】:

在 C++0x 中更改的 AFAIK。

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

如果有,您可以查看相关的 DR(缺陷报告)。

编辑:检查这一点,我发现其他人也相信这一点,但没有人能够在标准草案中找到任何这样的支持。 This SO thread 似乎表明C++0x 不支持函数模板的部分特化

编辑 2:只是我所说的“将函数放置为类的 static 成员”的一个示例:

#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
}

【讨论】:

  • 你有n3225的标准吗?我做了一个快速搜索,但找不到它:/
  • 啊抱歉……少了一个字。我有文件,但找不到特定的段落。尽管给出了您的编辑,但我想这仅仅是因为它不在其中:)
  • 这在 C++0x 中没有改变。我也怀疑它的实用性。您始终可以重载模板并使用部分 ordering
  • 后期更新:八年后即使在C++17也没有改变,似乎也没有进入C++20。不过,看不出有什么理由……
  • 我相信这是迄今为止最全面的概念实现
【解决方案2】:

好吧,你确实不能做部分函数/方法的特化,但是你可以做重载。

template <typename T, typename U>
T fun(U pObj){...}

// acts like partial specialization <T, int> AFAIK 
// (based on Modern C++ Design by Alexandrescu)
template <typename T>
T fun(int pObj){...} 

就是这样,不知道能不能满足你。

【讨论】:

  • 哇,我的脑子里充满了模板,以至于我真的忘记了事情是多么简单:)
  • 不幸的是,当您想在部分专门化函数后传递可变参数时,情况并非如此...... :(
  • 我不确定传递可变参数模板是什么意思,所以我想知道它与部分专业化有何不同。您能否提供更多详细信息?
  • 如果您只需要两个函数用于所有整数和浮点类型怎么办?
【解决方案3】:

一般来说,完全不建议专门化函数模板,因为重载的麻烦。这是来自 C/C++ 用户杂志的一篇好文章:http://www.gotw.ca/publications/mill17.htm

它包含对您问题的诚实回答:

一方面,你不能对它们进行部分专业化——几乎只是因为语言说你不能。

【讨论】:

  • 这篇文章除了提到一次外,不是偏专业化的。
【解决方案4】:

由于您可以部分特化类,因此可以使用仿函数:

#include <iostream>

template < typename dtype , int k > struct fun
{
 int operator()()
 {
  return k ;
 }
} ;

template < typename dtype > struct fun < dtype , 0 >
{
 int operator()()
 {
  return 42 ;
 }
} ;

int main ( int argc , char * argv[] )
{
 std::cout << fun<float,5>()() << std::endl ;
 std::cout << fun<float,0>()() << std::endl ;
}

【讨论】:

  • 然后您可以使用单个函数模板进行调用,摆脱丑陋的()() 语法。
猜你喜欢
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2011-06-27
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多