函数模板专业化格式

发布于 2024-07-23 02:30:46 字数 1728 浏览 13 评论 0原文

第二个括号<>的原因是什么? 在以下函数模板中:

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

这出现在SO问题中建议的地方operator() 之后缺少括号,但是我找不到解释。

如果它是以下形式的类型专业化(完全专业化),我理解其含义:

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.1)使用 3):

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

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

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

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

However for function templates:

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

Where does this fit into this scenarion?:

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

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);
}

Output:

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

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

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

发布评论

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

评论(1

稀香 2024-07-30 02:30:46

我查了一下,发现是14.5.2/2指定的:

本地类不得有成员模板。 访问控制规则(第 11 条)适用于成员模板名称。 析构函数不应是成员模板。 具有给定名称和类型的普通(非模板)成员函数和同名的成员函数模板(可用于生成相同类型的特化)都可以在类中声明。 当两者都存在时,除非提供显式模板参数列表,否则对该名称和类型的使用将引用非模板成员。

它提供了一个示例:

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
}

请注意,在标准术语中,特化 指的是使用显式特化编写的函数以及使用实例化生成的函数,在这种情况下,我们必须处理生成的特化。 专业化不仅仅指您使用显式专业化模板创建的函数(它通常仅用于该模板)。

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

“ComeauTest.c”,第 16 行:错误:“void doh::operator()(bool)” 不是一个实体,
可以明确专门化
模板 void doh::operator()(bool i)

请注意,它并没有抱怨 int 模板的专门化(仅适用于 bool),因为它不引用相同的名称​​和类型:特化所具有的函数类型是void(int),它与非特化的函数类型不同- 模板成员函数,即void(bool)

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

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.

And it provides an example:

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
}

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.

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

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文