haster8558
haster8558

Reputation: 463

Template and const

I guess what I'm trying to do it's impossible but I have this code:

 22 class Q
 21 {   
 22     int integer;
 23     int fractional;
 24     int word;
 25   public:
 26     Q(int i,int f) { this->integer = i; this->fractional = f; this->word = i+f;}
 27     const int get_i() {return (const int)this->integer;}
 28     const int get_f() {return (const int)this->fractional;}
 29     const int get_w() {return (const int)this->word;}
 30     
 31     friend ostream& operator<<(ostream& os, const Q& q){ os << "Q" << q.integer << "." << q.fractional << " (w:" << q.word << ")"; return os; }
 32 };
 33 
 34 
 35 const Q Q1_i = Q(1,10);
 36 const Q Q1_o = Q(0,11);
 37 
 38 const std::array<Q,1> input_queue_q = {Q1_i};
 39 const std::array<Q,1> output_queue_q = {Q1_o};
 40 
 41 
 42 int sc_main(int argc, char *argv[])
 43 { 
 44   ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
 45   
 46   for(const Q &input_q : input_queue_q)
 47   { 
 48     for(const Q &output_q : output_queue_q)
 49     { 
 50       ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
 51     }
 52   }
 53 }

When I try to compile I got the following error:

check_ac_one_over.cpp: In function ‘int sc_main(int, char**)’:
check_ac_one_over.cpp:44:23: error: passing ‘const Q’ as ‘this’ argument of ‘const int Q::get_w()’ discards qualifiers [-fpermissive]
   ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
                       ^
check_ac_one_over.cpp:44:23: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:44:23: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:44:46: note: in template argument for type ‘int’ 
   ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
                                              ^
check_ac_one_over.cpp:44:51: error: invalid type in declaration before ‘;’ token
   ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
                                                   ^
check_ac_one_over.cpp:50:30: error: passing ‘const Q’ as ‘this’ argument of ‘const int Q::get_w()’ discards qualifiers [-fpermissive]
       ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
                              ^
check_ac_one_over.cpp:50:30: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:50:30: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:50:53: note: in template argument for type ‘int’ 
       ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
                                                     ^
check_ac_one_over.cpp:50:58: error: invalid type in declaration before ‘;’ token
       ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
                                                          ^
make: *** [check_ac_one_over.o] Error 1

I guess the problem is that gcc think that Q1_i.get_w() can change. Is there a way to compile this code ? I would like to iterate and use that templated type using the class that I've defined above.

Cheers, Stefano.

Upvotes: 0

Views: 98

Answers (1)

John Zwinck
John Zwinck

Reputation: 249582

You need to make your code "const correct". Currently it is:

const int get_w() {return (const int)this->word;}

Returning a value as const is useless, because modifications to a value do not reflect anywhere else. But you failed to make the method const, meaning it does not change this. It should be:

int get_w() const {return word;}

The const following the parentheses means that the method will not modify any member variables (except those marked mutable, which are usually none).

Upvotes: 4

Related Questions