从variadic函数参数传递给构造函数的声明和初始化std ::元组
我正在尝试创建一个std ::元组的几个大对象,然后我可以随后使用,理想情况下可以访问他们的方法并从texturemanager中进行其他操作,
class TextureManager
{
public:
template<typename T , typename... Args>
TextureManager(T t, Args... args)
:graphical_objects{std::make_tuple(t, args...)}
{}
// How to declare graphical_objects ?
std::tuple< /*what to put here*/ > graphical_objects;
};
int main()
{
TextureManager tm1(1, 2, 'a');
// This is how I'd like to use TextureManager
TextureManager text_manager2(my_graphics_obj1, my_graphics_obj2, my_graphics_obj3);
// Or how ever many parameters...
return 0;
}
这是My_graphics_obj1的玩具示例
class GraphicsObject
{
virtual void CreateTextures() = 0;
};
class StringTexture: public GraphicsObject
{
SDL_Texture* CreateTextures (/*params*/)
{
// do rendering and what not
return A_SDL_Texture*;
}
};
,理想情况下,我可以访问CreatseTextures ()使用元组graphical_objects。 这甚至可能吗?
我几乎谷歌搜索了“成员”,“元组”,“ variadic模板”,“从”和“使用”的“提取元素”的所有可能的单词组合。 我很确定不是这个问题 https://stackoverflow.com/questions/34191963/how-i-i-declare-a-stduple-using-using-variadic-templates"> or this 我不会链接到我查看的所有帖子,以节省所有关注的时间。 我觉得答案很简单,或者我完全错了,而且更多的参与。
我确实可以访问C ++ 17,目前我正在使用Clang编译(对于更好的错误消息),但这在MO的C ++ 14。两者中的解决方案都可以。
I am trying to create a std::tuple of several large objects, that I can then subsequently use, ideally to access their methods and do other stuff from within TextureManager
class TextureManager
{
public:
template<typename T , typename... Args>
TextureManager(T t, Args... args)
:graphical_objects{std::make_tuple(t, args...)}
{}
// How to declare graphical_objects ?
std::tuple< /*what to put here*/ > graphical_objects;
};
int main()
{
TextureManager tm1(1, 2, 'a');
// This is how I'd like to use TextureManager
TextureManager text_manager2(my_graphics_obj1, my_graphics_obj2, my_graphics_obj3);
// Or how ever many parameters...
return 0;
}
Here's a toy example of my_graphics_obj1
class GraphicsObject
{
virtual void CreateTextures() = 0;
};
class StringTexture: public GraphicsObject
{
SDL_Texture* CreateTextures (/*params*/)
{
// do rendering and what not
return A_SDL_Texture*;
}
};
Ideally, I'd be able to access CreateTextures() using the tuple graphical_objects.
Is this even possible ?
I have googled just about every possible word combination of 'member','tuple','variadic template', 'extract elements from' and 'declare using' I can think of.
I'm fairly sure it's not this question or this one
I won't link to all the posts I've looked at, to save all concerned some time.
I feel the answer is something simple, or I've got it totally wrong, and it's a lot more involved.
I do have access to C++17, at the moment I'm compiling with clang (for nicer error messages) but that's at C++14 at the mo. A solution in both/either would be fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将自己进入一年的早期棕榈,
以下几个现在可以按照C ++ 17的预期工作。
感谢您的各种评论,使我进入了解决方案。
我还以为我会更改#包括&lt; vector&gt; to &lt; tuple&gt; 大约4小时前;原来我没有。
graphical_objects 现在让我访问传递给构造函数的对象。
I'm going to enter myself for an early Face Palm of the Year
The following now works as expected in C++17.
Thanks for the various comments that got me to the solution.
I also thought I'd changed #include<vector> to <tuple> about 4 hrs ago; turns out I hadn't.
graphical_objects now gives me access to the objects I pass to the constructor.