我試圖讓使用std::reference_wrapper
這些類的多態矢量:的std ::的reference_wrapper和多態容器
struct Int2TypeBase{
virtual void which(){ std::cout << "Int2TypeBase" << "\n";}
};
template <int v>
struct Int2Type : public Int2TypeBase
{
enum
{
value = v
};
void which(){ std::cout << "Int2Type<" << value << ">""\n";}
friend bool operator==(const Int2Type& lhs, const Int2Type& rhs){
return lhs.v == rhs.v;
}
};
現在我想利用std::reference_wrapper
這樣的:
int main(){
using namespace std;
std::vector<std::reference_wrapper<Int2TypeBase>> v;
Int2Type<0> i2t_1;
v.emplace_back(i2t_1);
auto x = v[0];
x.get().which();
std::cout << typeid(x.get()).name() << "\n";
// std::cout << (x.get() == i2t_1) << "\n";
}
輸出是:
Int2Type<0>
8Int2TypeILi0EE
這是我所期望的。
但是現在,當我取消std::cout << (x.get() == i2t_1) << "\n";
我會得到
invalid operands to binary expression ('Int2TypeBase' and 'Int2Type<0>')
這讓我困惑,因爲typeid(x.get()).name()
返回8Int2TypeILi0EE
而不是F12Int2TypeBasevE
這是我得到typeid(Int2TypeBase()).name();
。此外which()
也被稱爲派生類...那麼爲什麼x.get()
x.get() == i2t_1
評估爲Int2TypeBase
?
有沒有另外一種方法在編譯時獲取派生對象的類型? –
@lotolmencre - 不,實際上不可能。當你的集合被聲明爲持有基類型時,就編譯器而言,它可能包含任何派生類型。你想做的任何事情都是由完整類型決定的,它必須是一個虛擬方法(或者使用其他形式的動態分派) – antlersoft