jeudi 14 juin 2018

Class object as template funciton argument

I have an issue with the templates and my knowledge about them is definitely limited. So I have a class which should store some information:

class Q
{
    int integer;
    int fractional;
  public:
    constexpr Q(int i,int f) : integer(i),fractional(f) {}
    int get_i() const {return this->integer;}
    int get_f() const {return this->fractional;}
    constexpr int get_w() {return this->integer + this->fractional;}

    friend ostream& operator<<(ostream& os, const Q& q){ os << "Q" << q.integer << "." << q.fractional << " (w:" << q.integer + q.fractional << ")"; return os; }
};

Then I have my templated function. This is just an example but it show the point:

template <Q input_q, Q output_q,const unsigned int X_0_evaluated_bit> void calculate_stuff (const int max_iterations)
{ 
  std::array<Q,input_q.get_w()> input_queue_q;
}

And at the end the main (I'm using the SystemC library) where I generate the constant object Q that I want to use in the function

int sc_main(int argc, char *argv[])
{
  constexpr Q Q1_i = Q(1,10);
  constexpr Q Q1_o = Q(0,11);

  // Number of bits used to address the LUT for the initial value
  const unsigned int X_0_evaluated_bit = 5;

  // Number of iteration for the Newton-Raphson
  const int max_iterations = 2;

  calculate_stuff <Q1_i,Q1_o,X_0_evaluated_bit> (max_iterations);
  return 0;
}

If I try to compile I get the following error message:

check_ac_one_over.cpp:31:13: error: ‘class Q’ is not a valid type for a template non-type parameter
 template <Q input_q, Q output_q,const unsigned int X_0_evaluated_bit> void calculate_stuff (const int max_iterations)
             ^
check_ac_one_over.cpp:31:24: error: ‘class Q’ is not a valid type for a template non-type parameter
 template <Q input_q, Q output_q,const unsigned int X_0_evaluated_bit> void calculate_stuff (const int max_iterations)
                        ^
check_ac_one_over.cpp: In function ‘void calculate_stuff(int)’:
check_ac_one_over.cpp:33:31: error: template argument 2 is invalid
   std::array<Q,input_q.get_w()> input_queue_q;
                               ^
check_ac_one_over.cpp:33:46: error: invalid type in declaration before ‘;’ token
   std::array<Q,input_q.get_w()> input_queue_q;
                                              ^
check_ac_one_over.cpp: In function ‘int sc_main(int, char**)’:
check_ac_one_over.cpp:102:64: error: no matching function for call to ‘calculate_stuff(const int&)’
   calculate_stuff <Q1_i,Q1_o,X_0_evaluated_bit> (max_iterations);
                                                                ^
check_ac_one_over.cpp:102:64: note: candidate is:
check_ac_one_over.cpp:31:76: note: template<<typeprefixerror>input_q, <typeprefixerror>output_q, unsigned int X_0_evaluated_bit> void calculate_stuff(int)
 template <Q input_q, Q output_q,const unsigned int X_0_evaluated_bit> void calculate_stuff (const int max_iterations)
                                                                            ^
check_ac_one_over.cpp:31:76: note:   template argument deduction/substitution failed:
check_ac_one_over.cpp:102:64: note: invalid template non-type parameter
   calculate_stuff <Q1_i,Q1_o,X_0_evaluated_bit> (max_iterations);
                                                                ^
check_ac_one_over.cpp:102:64: note: invalid template non-type parameter
make: *** [check_ac_one_over.o] Error 1

Now I'm not sure if what I'm trying to do is possible. Does anyone have some ideas how can I make it work ?

Cheers,

Stefano

Aucun commentaire:

Enregistrer un commentaire