小编Fun*_*tic的帖子

在成员函数返回类型上参数化的类模板部分特化

以下代码尝试根据成员函数指针类型的返回类型来专门化类模板'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)

c++ templates member-function-pointers partial-specialization visual-c++

6
推荐指数
1
解决办法
1279
查看次数