compile error happen when I use c++ template. it is from a third part library.
there is a template class case_wrapper_base, when I create a instance with the type int
, there will be compile error, complaining that const type can not use default initialization.
template <class CT>
class case_wrapper_base
{
public:
explicit case_wrapper_base(const CT& v) : check(v), default_step(nullptr) {}
case_wrapper_base& add_entry(const case_instruction& lambda_holder)
{
steps.push_back(&lambda_holder);
return *this;
}
case_wrapper_base& add_default(const case_instruction& lambda_holder)
{
default_step = &lambda_holder;
return *this;
}
case_wrapper_base& join()
{
return *this;
}
void run() const
{
auto it = steps.begin();
while(it != steps.end())
{
bool increased = false;
// error happens due to the two different type here
if(dynamic_cast<const branch<CT>*>(*it) || dynamic_cast<const branch<const CT>*>(*it))
// do something. not important
}
the definition of class branch
is
template<class CT>
class branch final : public case_instruction
{
public:
template<class T>
branch(T lambda) {condition.reset(new any_functor<T>(lambda));}
bool equals(const base_rvholder& rv, CT lv) const
{
return rv.equals(lv);
}
virtual next_step execute(const base_rvholder& against) const override
{
// there is compile error here. Default initialization of an object of const type 'const int'. I know it is because the type of `CT` become `const int`. but why there is no error when build with Xcode15. can I make it no error by adding some settings in Xcode16?
CT retv;
condition->run( const_cast<void*>(reinterpret_cast<const void*>(&retv) ) );
return equals(against,retv) ? next_step::ns_done : next_step::ns_continue;
}
private:
std::unique_ptr<any_functor_base> condition;
};
@DTS Engineer appreciate your reply.