Fun*_*tic 6 c++ templates member-function-pointers partial-specialization visual-c++
以下代码尝试根据成员函数指针类型的返回类型来专门化类模板'special',导致VC9编译错误:
template<class F> struct special {};
template<class C> struct special<void(C::*)()> {};
template<class R, class C> struct special<R(C::*)()> {};
struct s {};
int main()
{
special<void(s::*)()> instance;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误C2752:'special':多个部分特化匹配模板参数列表
GCC-4.3.4接受相同的代码,如下所示:http ://ideone.com/ekWGg
这是VC9中的一个错误,如果是这样,这个错误是否仍然存在于VC10中?
然而,我提出了一个可怕的侵入式解决方法(对于这个特定的用例,至少.欢迎更一般的解决方案):
#include <boost/function_types/result_type.hpp>
#include <boost/type_traits/is_same.hpp>
template<typename F, typename R>
struct is_result_same :
boost::is_same<
typename boost::function_types::result_type<F>::type,
R
>
{};
template<class F, bool = is_result_same<F, void>::value>
struct special {};
template<class R, class C> struct special<R(C::*)(), true> {};
template<class R, class C> struct special<R(C::*)(), false> {};
Run Code Online (Sandbox Code Playgroud)
这是一个错误。
template <class C> struct special<void(C::*)()>; // specialization 1
template <class R, class C> struct special<R(C::*)()>; // specialization 2
Run Code Online (Sandbox Code Playgroud)
根据 14.5.4.2,这两个类模板特化的偏序与这些虚函数模板的偏序相同:
template <class C> void f(special<void(C::*)()>); // func-template 3
template <class R, class C> void f(special<R(C::*)()>); // func-template 4
Run Code Online (Sandbox Code Playgroud)
根据 14.5.5.2,这两个函数模板的部分顺序是通过用发明的类型替换一个函数模板的参数列表中的每个类型模板参数并尝试使用另一个函数模板中的该参数列表进行模板参数推导来确定的。
// Rewrite the function templates with different names -
// template argument deduction does not involve overload resolution.
template <class C> void f3(special<void(C::*)()>);
template <class R, class C> void f4(special<R(C::*)()>);
struct ty5 {}; struct ty6 {}; struct ty7 {};
typedef special<void(ty5::*)()> arg3;
typedef special<ty6 (ty7::*)()> arg4;
// compiler internally tests whether these are well-formed and
// the resulting parameter conversion sequences are "exact":
f3(arg4());
f4(arg3());
Run Code Online (Sandbox Code Playgroud)
模板实参推导的详细信息参见14.8.2。有效扣除额包括template_name<dependent_type>
和dependent_type1 (dependent_type2::*)(arg_list)
。所以f4(arg3())
推演成功,推导出f4<void,ty5>(arg3());
。演绎f3(arg4())
显然永远不会成功,因为void
和ty6
不统一。
因此,函数模板 3 比函数模板 4 更专业。类模板专业化 1 比类模板专业化 2 更专业。因此,尽管special<void(s::*)()>
匹配两个专业化,但它明确实例化了专业化 1。
归档时间: |
|
查看次数: |
1279 次 |
最近记录: |