功能模板专业化格式 [英] Function template specialization format

查看:197
本文介绍了功能模板专业化格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下函数模板中的第二个括号

What is the reason for the second brackets <> in the following function template:

template<> void doh::operator()<>(int i)

href =http://stackoverflow.com/questions/937107/do-template-specializations-require-template-syntax> SO问题其中建议在后缺少括号,

This came up in SO question where it was suggested that there are brackets missing after operator(), however I could not find the explanation.

我理解的意思,如果它是一个类型专业化(全特化)的形式:

I understand the meaning if it was a type specialization (full specialization) of the form:

template< typename A > struct AA {};
template<> struct AA<int> {};         // hope this is correct, specialize for int

但是对于功能模板:

template< typename A > void f( A );
template< typename A > void f( A* ); // overload of the above for pointers
template<> void f<int>(int);         // full specialization for int

这里适合这个场景?:

template<> void doh::operator()<>(bool b) {}

工作和不给任何警告/错误(gcc 3.3.3使用):

Example code that seems to work and does not give any warnings/error (gcc 3.3.3 used):

#include <iostream>
using namespace std;

struct doh
{
    void operator()(bool b)
    {
        cout << "operator()(bool b)" << endl;
    }

    template< typename T > void operator()(T t)
    {
        cout << "template <typename T> void operator()(T t)" << endl;
    }
};
// note can't specialize inline, have to declare outside of the class body
template<> void doh::operator()(int i)
{
    cout << "template <> void operator()(int i)" << endl;
}
template<> void doh::operator()(bool b)
{
    cout << "template <> void operator()(bool b)" << endl;
}

int main()
{
    doh d;
    int i;
    bool b;
    d(b);
    d(i);
}

输出:

operator()(bool b)
template <> void operator()(int i)


推荐答案

查找它,并发现它由14.5.2 / 2指定:

I've looked it up, and found that it is specified by 14.5.2/2:


本地类不应具有成员模板。访问控制规则(第11节)适用于成员模板名称。析构函数不应是成员模板。具有给定名称和类型的正常(非模板)成员函数以及可用于生成相同类型的专门化的同名的成员函数模板都可以在类中声明。当两者都存在时,使用该名称和类型是指非模板成员,除非提供了显式模板参数列表。

A local class shall not have member templates. Access control rules (clause 11) apply to member template names. A destructor shall not be a member template. A normal (non-template) member function with a given name and type and a member function template of the same name, which could be used to generate a specialization of the same type, can both be declared in a class. When both exist, a use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

它提供了一个例子:

template <class T> struct A {
    void f(int);
    template <class T2> void f(T2);
};

template <> void A<int>::f(int) { } // non-template member
template <> template <> void A<int>::f<>(int) { } // template member

int main()
{
    A<char> ac;
    ac.f(1); //non-template
    ac.f(’c’); //template
    ac.f<>(1); //template
}

请注意,在标准术语中, 是指使用显式专门化编写的函数,以及使用实例化生成的函数,在这种情况下,我们必须使用生成的专门化。 specialization 不仅指您使用显式专门化模板创建的函数,而且通常只使用它。

Note that in Standard terms, specialization refers to the function you write using an explicit specialization and to the function generated using instantiation, in which case we have to do with a generated specialization. specialization does not only refer to functions you create using explicitly specializing a template, for which it is often only used.

结论:GCC错误。 Comeau,我也测试了代码,得到它正确,并发出诊断:

Conclusion: GCC gets it wrong. Comeau, with which i also tested the code, gets it right and issues a diagnostic:


ComeauTest.c ,第16行:错误:void doh :: operator()(bool)不是一个实体,
可以显式专用
template<> void doh :: operator()(bool i)

"ComeauTest.c", line 16: error: "void doh::operator()(bool)" is not an entity that can be explicitly specialized template<> void doh::operator()(bool i)

请注意, int (仅适用于 bool )的模板的专用化,因为它不引用相同的名称 type:特殊化的函数类型是 void(int),它与非模板成员函数的函数类型不同,是 void(bool)

Note that it isn't complaining about the specialization of the template for int (only for bool), since it doesn't refer to the same name and type: The function type that specialization would have is void(int), which is distinct from the function type of the non-template member function, which is void(bool).

这篇关于功能模板专业化格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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