Cant cast template argument

SOLVED

Im getting this error at the static cast line

​

Error	C2440	'static_cast': cannot convert from 'ComponentType *' to 'ComponentStoreType *'

​

I get the exact same error from using dynamic_cast or reinterpret_cast, other than the cast name.

​

Any ideas?

​

template <typename ComponentType>
bool insertComponent(const Entity& entity, ComponentType& component) { 

    static_assert(std::is_base_of<ComponentStoreType, ComponentType>::value, "ComponentType must be derived from ComponentStoreType!");

    if (storesComponentType<ComponentType>())
    {
        insertComponentVirtual(entity, static_cast<ComponentStoreType*>(&component));
        return true;
    }
    else {
        return false;
    }
};

insertComponentVirtual is not templated and looks like

virtual void insertComponentVirtual(const Entity& entity, ComponentStoreType* component) = 0;

​

Edit: Solved by changing second parameter to be a const reference (To accept both lvalue and rvalue references). Also letting the call to insertComponentVirtual do an implicit conversion, and having insertComponentVirtual accept "pointer to const", instead of just a pointer as it were before.

template <typename ComponentType>
bool insertComponent(const Entity& entity, const ComponentType& component) {
	
    static_assert(std::is_base_of<ComponentStoreType, ComponentType>::value, "ComponentType must be derived from ComponentStoreType!");

    if (storesComponentType<ComponentType>())
    {
	insertComponentVirtual(entity, &component);
	return true;
    }
    else {
	return false;
    }
};

​

virtual void insertComponentVirtual(const Entity& entity, ComponentStoreType const* component) = 0;

Posted Apr 19, 2019, 9:36 PM UTC · edited

19
ihamsa•

Why do you need to cast? ComponentType is derived from ComponentStoreType. There should be an implicit conversion.

2
NazozOP•

Thats what i did first, but i got this error

Error	C2664	'void ComponentManagerBase::insertComponentVirtual(const Entity &,ComponentStoreType *)': cannot convert argument 2 from 'ComponentType *' to 'ComponentStoreType *'

Edit: On the same line

1
ihamsa•

You need to show more context. Try creating a standalone example that reproduces the error.

1
NazozOP•

Alright, so i did this:

struct ComponentStoreType {};

struct Derived : public ComponentStoreType
{
	int i;
	Derived(int i) : i(i) {};
};

class ComponentManagerBase
{
	virtual void insertComponentVirtual(const Entity& entity, ComponentStoreType* component) = 0;
public:
	template <typename ComponentType>
	void insertComponent(const Entity& entity, ComponentType& component) {
		insertComponentVirtual(entity, &component);
	};
};

template <typename ComponentType>
class ComponentManager : public ComponentManagerBase
{
public:
	
	void insertComponentVirtual(const Entity& entity, ComponentStoreType* component) override
	{

	}

	ComponentManager() { };
	~ComponentManager() { };
};

int main()
{
	ComponentManagerBase* manager = new ComponentManager<Derived>;
	manager->insertComponent(Entity{ 1 }, Derived{ 5 });
}

Error on manager->insertComponent line which is very similar to the one i was getting inside insertComponent:

Error	C2664	'void ComponentManagerBase::insertComponent<Derived>(const Entity &,ComponentType &)': cannot convert argument 2 from 'Derived' to 'ComponentType &'	

But this type i also get the error:

Error (active)	E0304	no instance of function template "ComponentManagerBase::insertComponent" matches the argument list

If i change the main to:

int main()
{
	ComponentManagerBase* manager = new ComponentManager<Derived>;
	Derived dt{ 5 };
	manager->insertComponent(Entity{ 1 }, dt);
}

i get no errors...

1
avicenna_t•

insertComponent takes a lvalue reference, but you are passing it a temporary Derived{ 5 }.

3
ihamsa•

You are trying to pass a temporary Derived{5} by non-const reference. This is not allowed.

1