0

In the following code, how to use the list b, to create object_b in the same way that the list a was used to create object_a manually?

#include <list>

template <int...Args>
class Object {};

int main() {
    std::list<int> a = {1,2,3,4,5};
    Object<1,2,3,4,5> object_a;
    std::list<int> b;
      // do whatever with b
    // Object< ? > object_b;  // how to use b to create object_b?
}
prestokeys
  • 4,817
  • 3
  • 20
  • 43

2 Answers2

0

My best solution then:

#include <list>

template <std::list<int>& L>
class Object {};

std::list<int> globalList;

int main() {
    std::list<int> a = {1,2,3,4,5};
    globalList = a;
    Object<globalList> object_a;
}

Any better ideas? Any way without introducing a global variable?

prestokeys
  • 4,817
  • 3
  • 20
  • 43
  • Well, I managed to create the object with the template parameters using the elements of a. That I changed the nature of the template is fine for my needs, as long as I can use those elements within the class to do whatever. But I don't like global variable entering the picture now. – prestokeys Mar 15 '14 at 21:59
  • I think 0x499602D2's suggestion of using std::tuple will avoid creating a global variable, right? – prestokeys Mar 15 '14 at 22:05
  • @ CantChooseUserNames. Sorry I didn't see your link. I appreciate you typing out that code for me. Thanks very much. – prestokeys Mar 15 '14 at 23:14
0

Are you asking to be able to construct your Object using the same syntax as std::list?

You can use std::initializer_list to achieve this.

class Object{

    Object(const std::initialize_list<int>& vars)
    {
          // do stuff with the vars. 
    }
};

You can then do

Object my_object = {1, 2, 3, 4}; 
capturesteve
  • 373
  • 2
  • 10
  • No. Object is not a single class, but a family of classes parametrized by (arbitrarily many) ints, in whatever form. Then I want to create an instance of a specific class of the Object family using a known list (or tuple or whatever) of ints. – prestokeys Mar 15 '14 at 22:11