는 자세한하지만 당신은 이런 식으로 작업을 수행 할 수 있습니다 :이 표준에 대한 좋은 지원하는 컴파일러 (가장 최근의 컴파일러가 잘되어야하지만, 비주얼 C의 이전 버전은 ++되지 않습니다)가 필요합니다
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_of.hpp>
struct base {};
template <typename ImplementationClass, class Enable = void>
class WrapperClass;
template <typename ImplementationClass>
class WrapperClass<ImplementationClass,
typename boost::enable_if<
boost::is_base_of<base,ImplementationClass> >::type>
{};
struct derived : base {};
struct not_derived {};
int main() {
WrapperClass<derived> x;
// Compile error here:
WrapperClass<not_derived> y;
}
. 자세한 내용은 Boost.Enable_If documentation을 참조하십시오.
페루 치오 말했듯이
, 간단하지만 덜 강력한 구현 :
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_of.hpp>
struct base {};
template <typename ImplementationClass>
class WrapperClass
{
BOOST_STATIC_ASSERT((
boost::is_base_of<base, ImplementationClass>::value));
};
enable_if 대신 BOOST_STATIC_ASSERT를 사용하면 조금 더 명확해질 수 있습니다. 즉 BOOST_STATIC_ASSERT (boost :: is_base_of ); –
Ferruccio
네, 제가 추가했습니다. 그것은 당신이 다른 버전을 가지고 약간 좋은 오류 메시지 (IMO)를 제공하므로 enable_if를 선호합니다. 어떤 사람들은 MPL의 정적 주장의 오류 메시지도 좋아합니다. –
고마워, 저것은 일했다 :) – JSN