从variadic函数参数传递给构造函数的声明和初始化std ::元组

发布于 2025-01-21 12:14:49 字数 1509 浏览 3 评论 0原文

我正在尝试创建一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2025-01-28 12:14:49

我将自己进入一年的早期棕榈,

以下几个现在可以按照C ++ 17的预期工作。
感谢您的各种评论,使我进入了解决方案。
我还以为我会更改#包括&lt; vector&gt; to &lt; tuple&gt; 大约4小时前;原来我没有。


#include <iostream>
#include <tuple>

class GraphicsObject
{
  virtual void CreateTextures() = 0;
};

class MandelbrotTexture: public GraphicsObject
{
  float mtf = 78.4;
  void CreateTextures (/*params*/)
  {
    std::cout<<"returning SDL_Texture or GTexure\n";
    return;
  }
};

// template declaration should be here, not where it was...
template<typename T, typename... Args>
class TextureManager
{
  public:

// template<typename T, typename... Args>  this was the wrong place !

  TextureManager(T t, Args... args)
                :graphical_objects(std::make_tuple(t, args...))  // No {} !
  {}

// I'd also forgotten 'T,' and was just trying 'Args...', also wrong.

  std::tuple<T, Args...> graphical_objects;
};

int main(int argc, char const *argv[])
{
  MandelbrotTexture mt;
  TextureManager test(1, 2, 'a');
  TextureManager test2(mt, mt, mt, mt);
  return 0;
}

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.


#include <iostream>
#include <tuple>

class GraphicsObject
{
  virtual void CreateTextures() = 0;
};

class MandelbrotTexture: public GraphicsObject
{
  float mtf = 78.4;
  void CreateTextures (/*params*/)
  {
    std::cout<<"returning SDL_Texture or GTexure\n";
    return;
  }
};

// template declaration should be here, not where it was...
template<typename T, typename... Args>
class TextureManager
{
  public:

// template<typename T, typename... Args>  this was the wrong place !

  TextureManager(T t, Args... args)
                :graphical_objects(std::make_tuple(t, args...))  // No {} !
  {}

// I'd also forgotten 'T,' and was just trying 'Args...', also wrong.

  std::tuple<T, Args...> graphical_objects;
};

int main(int argc, char const *argv[])
{
  MandelbrotTexture mt;
  TextureManager test(1, 2, 'a');
  TextureManager test2(mt, mt, mt, mt);
  return 0;
}

graphical_objects now gives me access to the objects I pass to the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文