简体   繁体   中英

Declaring and Initialising std::tuple from Variadic Function Parameters Passed to Constructor

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM