I am trying to initialize a class with a pack passed as an argument to my function. Here is what I got so far:
struct Vec3
{
float x, y, z;
};
template<typename _Ty, typename... Args>
__forceinline _Ty construct_class(Args&&... arguments)
{
return _Ty(arguments...);
}
// here I am trying to construct a Vec3 by calling construct_class
Vec3 vec = construct_class<Vec3>(10.f, 20.f, 30.f);
Unfortunately I get the following compiler error in visual studio: C2440 '<function-style-cast>': cannot convert from 'initializer list' to '_Ty'
I have seen people doing this:
template<typename _Ty, typename... Args>
__forceinline _Ty construct_class(Args&&... arguments)
{
return _Ty(std::forward<Args>(arguments)...);
}
But this also doesn't work for me, I get the exact same compiler error as before.
So my question is how should I use the pack/intializer list inside construct_class to construct the _Ty
class (in this case Vec3
)? I am not familiar with packs in C++.