0

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Lozminda
  • 45
  • 8
  • 1
    `TextureManager` is not a template, only its constructor is. Its member variables must have fixed types, not depending on template parameters of the constructor. – HolyBlackCat Apr 14 '22 at 21:03
  • You have to make the class a template. – NathanOliver Apr 14 '22 at 21:04
  • If you're using C++ 17, you should be able to have the compiler deduce type parameters for a template class `TextureManager` given an expression `TextureManager tm1(1, 2, 'a');` (see CTAD) – fabian Apr 14 '22 at 21:09
  • I think I might win face palm of the year...give me 20 mins or so pls – Lozminda Apr 14 '22 at 21:20

1 Answers1

0

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.

Lozminda
  • 45
  • 8