У меня есть следующий код в классеКомпилятор не может вывести специализированный шаблон в C++
struct ConstrType{};
struct ConstrType3{};
struct ConstrType2{};
struct ConstrType1{};
template<typename InType, typename OutType, typename ConstrType>
class getInterestRateIndex_impl{
public:
getInterestRateIndex_impl(){
std::cout << "Generic getInterestedRateIndex_impl instantiated. Failed" << std::endl;
// BOOST_STATIC_ASSERT(sizeof(ConstrType) == 0);
}
boost::shared_ptr<OutType> operator()() const{
return boost::shared_ptr<OutType>();
}
};
template<typename InType, typename OutType>
class getInterestRateIndex_impl<InType, OutType, ConstrType2>{
public:
getInterestRateIndex_impl(){
std::cout << "ConstrType2 getInterestedRateIndex_impl instantiated." << std::endl;
}
boost::shared_ptr<OutType> operator()() const{
return boost::shared_ptr<OutType>();
}
};
template<typename InType, typename OutType>
class getInterestRateIndex_impl<InType, OutType, ConstrType1>{
public:
getInterestRateIndex_impl(){
std::cout << "ConstrType1 getInterestedRateIndex_impl instantiated." << std::endl;
}
boost::shared_ptr<OutType> operator()() const{
return boost::shared_ptr<OutType>();
}
};
template<typename InType, typename OutType>
boost::shared_ptr<OutType> getInterestRateIndex() const{
// BOOST_STATIC_ASSERT(boost::is_base_of<OutType, InType>::value);
typedef typename
boost::mpl::if_
<
boost::is_same<InType, QuantLib::Libor>,
QuantLib::Libor,
boost::mpl::if_
<
boost::mpl::or_
<
boost::mpl::or_
<
boost::is_same<InType, QuantLib::Euribor>,
boost::is_same<InType, QuantLib::EURLibor>,
boost::is_base_of<QuantLib::Libor, InType>
>
>,
ConstrType2,
boost::mpl::if_
<
boost::mpl::or_
<
boost::is_base_of<QuantLib::Euribor, InType>,
boost::is_base_of<QuantLib::EURLibor, InType>
>,
ConstrType1,
ConstrType
>
>
>::type Type;
// std::cout << typeid(Type).name() << std::endl;
// throw std::exception(typeid(Type).name());
return getInterestRateIndex_impl<InType, OutType, Type>()();
}
Когда я создаю экземпляр класса и вызывать getInterestRateIndex<DerivedFromLiborType, BaseType>()
, компилятор не может выбрать специализацию. Когда я раскомментирую линию exception
, он может хотя обнаружить, что Type
после typedef
- ConstrType2
. Я пропустил что-нибудь, что может намекнуть компилятору на выбор правильной специализации?
PS: логика шаблона предполагают, чтобы сделать что-то вроде .....
if(T is Libor)
return LiborType
if(
or(
or(T = Euribor,
T = EURLibor),
is_base_of(T, Libor)
),
ConstrType2,
if(
or(is_base_of(T, Euribor),
is_base_of(T, EURLibor)),
ConstrType1,
ConstrType
)
)
Я делаю это потому, что мне нужно послать shared_ptr
в зависимости от типа входного сигнала, и требуемый базовый тип в shared_ptr обертке ,
ли компилятор излучающий ошибку? или это просто выбор неправильной специализации? – zdan
@zdan. MSVC компилирует его и выбирает класс шаблона по умолчанию. – leon
Глаза кровотечения. Можете ли вы кипятить это немного? –