Confusing errors from switching to taking parameter pack as const reference
I have this code which almost works as intended, basically i want a function that creates an entity without adding any components to it, and one that creates the entity AND adds components to it. All functions are in one class EntityManager.
Takes no components:
Entity newEntity()
{
Entity newEntity{ entityCounter };
entityCount++;
entityCounter++;
std::cout << "Called newEntity(void)" << std::endl;
return newEntity;
}
Takes components:
template <typename... Components>
Entity newEntity(Components... components)
{
Entity newEntity{ entityCounter };
entityCount++;
entityCounter++;
unpackAndStoreComponentsInManagers(newEntity, std::forward<Components>(components)...);
std::cout << "Called newEntity(Components)" << std::endl;
return newEntity;
}
Since it might be relevant, here's also unpackAndStoreComponentsInManagers
template <typename ComponentType>
void unpackAndStoreComponentsInManagers(const Entity& entity, const ComponentType& component)
{
newComponent(entity, component);
}
template <typename ComponentType, typename... Rest>
void unpackAndStoreComponentsInManagers(const Entity& entity, const ComponentType& component, const Rest&... rest)
{
newComponent(entity, component);
unpackAndStoreComponentsInManagers(entity, rest...);
}
And also, here's the main
int main()
{
ComponentA a{ 0.0f, 1.0f, 2.0f };
ComponentB b{ true };
EntityManager e;
e.newEntity();
e.newEntity(a, b);
}
This works fine and prints as intended:
Called newEntity(void)
Called newEntity(Components)
Now here's the problem, i want to take the components parameter in newEntity, and instead take them as const references. Switching out newEntity that takes in components with this
template <typename... Components>
Entity newEntity(const Components&... components)
{
Entity newEntity{ entityCounter };
entityCount++;
entityCounter++;
unpackAndStoreComponentsInManagers(newEntity, std::forward<Components>(components)...);
std::cout << "Called newEntity(Components)" << std::endl;
return newEntity;
}
I get the errors:
Error 1:
Error C2665 'std::forward': none of the 2 overloads could convert all the argument types
Error 2:
Error C2672 'EntityManager::unpackAndStoreComponentsInManagers': no matching overloaded function found
Error 3:
Error C2780 'void EntityManager::unpackAndStoreComponentsInManagers(const Entity &,const ComponentType &,const Rest &...)': expects 3 arguments - 1 provided
Error 4:
Error C2780 'void EntityManager::unpackAndStoreComponentsInManagers(const Entity &,const ComponentType &)': expects 2 arguments - 1 provided
Error 1 and 2 are on the line in newEntity on line with std::forward in newEntity(Components).
Error 3 and 4 are on the declaration line of newEntity that takes components.
If i remove from main
e.newEntity(a, b);
i get no errors and it prints as expected
Called newEntity(void)
If i remove from main
e.newEntity();
i get the same errors, so the culprit is definitely the newEntity that adds components, but i cant figure out why...
Edit: Solved, thank you!
Changed newEntity(components) to
template <typename... Components>
Entity newEntity(const Components&... components)
{
Entity newEntity{ entityCounter };
entityCount++;
entityCounter++;
unpackAndStoreComponentsInManagers(newEntity, components...);
std::cout << "Called newEntity(Components)" << std::endl;
return newEntity;
}
Posted Apr 20, 2019, 6:30 PM UTC · edited
I’m guessing it doesn’t like the const reference.
Try to use forwarding references instead (
Components&&vsconst Components&): https://en.cppreference.com/w/cpp/utility/forwardAlright, so Components&& works, but then i loose the const which isnt ideal...
please read about forwarding references (previously also known as universal references); you have not lost constness if you give it a const object.
After seeing your other comment i started reading about it, still am. It's a bit difficult to understand, but i will keep at it! I will respond to your original comment as soon as i think i understand it.
It might better to just watch the man discuss template deduction rules:
https://www.youtube.com/watch?v=wQxj20X-tIU
it takes a while; you're wrapping your head around a few different concepts at the same time, all interacting with one another.
You should first familiarise your self with how template deduction is done:
This will give you
tas a const lvalue reference.If I have some:
If I give it a non-const lvalue-reference, it will deduce it's type such that
Twill be what it needs to be to give me aT const&. Just as if I had:it would work fine; except if you give it
cs, there is no wayTcan be deduced such thatS const&can be aT&; this is due to the constness.Next thing you need to familiarise yourself with is how references "collapse" with further reference. In essence they cancel out, such that a any reference of a type, is that reference of the type; if you are dealing with lvalue references, or want an lvalue reference, you're gonna get a damn lvalue reference; and an rvalue reference of an rvalue reference is just what you want, an rvalue reference.
Using
&&is not necessary for a forwarding reference to correctly work, if you know all the types that will ever be deduced, and know they will all not be needed ... but frankly, there's no point; just stick the&&as it will always do what you need for a forwarding reference.I give it a
int&&(which is like calling itf(10)) then the signature is deducted and you get the generated function:void g(int&&)which will keep the rvalue nature; and this is what you want (and is basically the same as by value for all it matters to you).if you give it a
int&then you getvoid g(int&); if you give it anint const&then you getvoid g(int const&);using
&&with a "raw" template parameter, no other things likeconst,volatile, or wrapped in things (likestd::vector<T>); thenTwill be the type you gave it exactly.This is where
std::forwardcomes in handy; as you now have a name for your object, if you pass it on, you will be giving an lvalue reference to it; thus,std::forwardallows you to give it to another function in the same form it was given to you.Edit: fixed typos, and changed naming for clarity