65,180
社区成员




template<class T, unsigned B>
class Base
{
//应被特化的函数
void Func(){}
};
template<class T, unsigned B>
struct Base
{
template <unsigned N>
typename ::enable_if<16!=N>::type
FuncImpl () { std::cout << "primary" << std::endl; }
template <unsigned N>
typename ::enable_if<16==N>::type
FuncImpl () { std::cout << "specialization" << std::endl; }
void Func() {
FuncImpl<B>();
}
};
#include <iostream>
namespace
{
template <bool,typename T,typename> struct conditional { typedef T type; };
template <typename T,typename U> struct conditional<false,T,U> {typedef U type; };
}
template<class T, unsigned B>
struct Base
{
void Func ()
{
typedef typename ::conditional<B!=16,primary_t,spec_t>::type type;
Func_impl(type());
}
private:
struct primary_t { };
struct spec_t { };
void Func_impl (primary_t) { std::cout << "primary" << std::endl; }
void Func_impl (spec_t ) { std::cout << "specialization" << std::endl; }
};
int main ()
{
{
Base<int,0> b;
b.Func();
}
{
Base<int,16> b;
b.Func();
}
return 0;
}