Я построения Entity-компонентов системы с помощью шаблона метапрограммирование я получаю либо Cannot convert from [base type] to [type user requested]&
или Cannot convert NullComponent to [type user requested]&
ошибки:.«Не удается преобразовать из 'A' до 'B &'
class Entity {
public:
Entity() = default;
~Entity() = default;
template<typename C, typename... Args>
void AddComponent(Args&&... args);
template<typename C>
C& GetComponent();
protected:
private:
//...add/get helper methods here...
unsigned int _id;
std::vector<std::unique_ptr<IComponent>> _components;
};
template<typename C>
C& Entity::GetComponent() {
for(auto c : _components) {
if(std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
return *c; //<-- error here
}
}
return NullComponent(); //<-- and here
}
EDIT
Эти варианты, кажется, работают в настоящее время.
template<typename C>
const C& Entity::GetComponent() const {
for(auto& uc : _components) {
auto* c = dynamic_cast<C*>(uc.get());
if(c && std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
return *c;
}
}
throw std::runtime_error(std::string("Component not available."));
}
ИЛИ
class Entity {
public:
//same as before...
protected:
private:
//same as before...
a2de::NullComponent _null_component;
};
template<typename C>
const C& Entity::GetComponent() const {
for(auto& uc : _components) {
auto* c = dynamic_cast<C*>(uc.get());
if(c && std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
return *c;
}
}
return _null_component;
}
анализом было бы легче, если вы вывесили полный образец, который вам хотите скомпилировать (в текущем коде отсутствуют компоненты, IComponent и NullComponent). – Rumburak
и вы забыли уважение 'c' в decltype? -> 'std :: is_same(). value' ->' std :: is_same (). value' –
В "кажется, работают сейчас «почему вы нуждаетесь в' std :: is_base_of' и 'std :: is_same'? Разве dynamic_cast не заботится обо всем, что вам нужно? Кроме того, я уверен, что ссылки в 'is_base_of' перестают корректно работать с кодом. – Rumburak