函数模板专业化格式
第二个括号<>的原因是什么? 在以下函数模板中:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我查了一下,发现是14.5.2/2指定的:
它提供了一个示例:
请注意,在标准术语中,
特化
指的是使用显式特化编写的函数以及使用实例化生成的函数,在这种情况下,我们必须处理生成的特化。专业化
不仅仅指您使用显式专业化模板创建的函数(它通常仅用于该模板)。结论:GCC 错了。 Comeau,我也用它测试了代码,得到正确的结果并发出诊断:
请注意,它并没有抱怨
int
模板的专门化(仅适用于bool
),因为它不引用相同的名称和类型:特化所具有的函数类型是void(int)
,它与非特化的函数类型不同- 模板成员函数,即void(bool)
。I've looked it up, and found that it is specified by 14.5.2/2:
And it provides an example:
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:
Note that it isn't complaining about the specialization of the template for
int
(only forbool
), since it doesn't refer to the same name and type: The function type that specialization would have isvoid(int)
, which is distinct from the function type of the non-template member function, which isvoid(bool)
.