【发布时间】:2015-10-29 12:14:56
【问题描述】:
我最近开始学习现代模板元编程,并为自己编写了一个类型的 index_of 函数。
template<std::size_t Index, class C, class A> struct mp_index_of_impl{};
template<std::size_t Index, class C,template<class...> class A,class ...Ts>
struct mp_index_of_impl<Index,C,A<C,Ts...>>{
using type = std::integral_constant<std::size_t,Index>;
};
template<std::size_t Index, class C,template<class...> class A,class ...Ts,class T1>
struct mp_index_of_impl<Index,C,A<T1,Ts...>>{
using type = typename mp_index_of_impl<Index + 1,C,A<Ts...>>::type;
};
template<std::size_t Index, class C,template<class...> class A> struct mp_index_of_impl<Index,C,A<>>{
//static_assert(false,"Does not contain type");
using type = std::integral_constant<std::size_t,0>;
};
问题是我最后的专长
template<std::size_t Index, class C,template<class...> class A>
struct mp_index_of_impl<Index,C,A<>>{
//throw a compile error here
};
我尝试像这样使用 static_assert
template<std::size_t Index, class C,template<class...> class A> struct mp_index_of_impl<Index,C,A<>>{
static_assert(false,"Does not contain type");
};
但是这样每次都会抛出编译错误,即使不匹配也是如此。
如果匹配此模板特化,我想抛出带有自定义错误消息的编译错误。我该怎么做?
【问题讨论】:
-
@101010 我试过了,但每次都会抛出。
标签: c++ templates c++11 template-meta-programming