Confusing errors from switching to taking parameter pack as const reference

SOLVED

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

317
Wh00ster•

I’m guessing it doesn’t like the const reference.

Try to use forwarding references instead (Components&& vs const Components&): https://en.cppreference.com/w/cpp/utility/forward

1
NazozOP•

Alright, so Components&& works, but then i loose the const which isnt ideal...

1
parnmatt•

please read about forwarding references (previously also known as universal references); you have not lost constness if you give it a const object.

1
NazozOP•

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.

1
Wh00ster•

It might better to just watch the man discuss template deduction rules:

https://www.youtube.com/watch?v=wQxj20X-tIU

3
parnmatt•

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:

template <typename T>
void f(T const& t);

This will give you t as a const lvalue reference.

If I have some:

struct S { /* ... */ };

S s;
S const cs;

f(s);  // T = S
f(cs);  // T = S

If I give it a non-const lvalue-reference, it will deduce it's type such that T will be what it needs to be to give me a T const&. Just as if I had:

template <typename T>
void g(T& t);

it would work fine; except if you give it cs, there is no way T can be deduced such that S const& can be a T&; 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.

T & -> T&
T && -> T&&
T& & -> T&
T& && -> T&
T&& & -> T&
T&& && -> T&&

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.

template <typename T>
void f(T&&);

I give it a int&& (which is like calling it f(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 get void g(int&); if you give it an int const& then you get void g(int const&);

using && with a "raw" template parameter, no other things like const, volatile, or wrapped in things (like std::vector<T>); then T will be the type you gave it exactly.

This is where std::forward comes 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::forward allows you to give it to another function in the same form it was given to you.

Edit: fixed typos, and changed naming for clarity

3
parnmatt•

std::forward<T>(value) is the same as static_cast<T&&>(value) (with a couple of extra overloads and static asserts)

Think about what you've written.

1
VirtueBot•

not exactly sure the issue but there are some red flags that i see

template <typename... Components>
Entity newEntity(Components... components) {
  // ...
  unpackAndStoreComponentsInManagers(newEntity, std::forward<Components>(components)...);      
}

if you want to use std::forward you should use a forwarding reference i.e. Components&&... components and at that point you should probably use forwarding references on your unpack function as well. at this point if feel the need to mention that imo std::forward is relatively difficult to understand and use correctly and often isnt even necessary.

in your case why not just make your new entity function const Components&... components without using std::forward at all?

1
NazozOP•

Thats a very good point! I think i got an error a couple of weeks ago where it fixed the problem to use std::forward, so i've just used it since haha.

I did as you said and just removed the std::forward, as use const Components&... components, and it works perfectly!

2
VirtueBot•

I did as you said and just removed the std::forward, as use const Components&... components, and it works perfectly!

ayyy nice!! (:

1