C++ variable to const expression

Posted by user1344784 on Stack Overflow See other posts from Stack Overflow or by user1344784
Published on 2011-01-01T05:17:30Z Indexed on 2011/01/01 5:53 UTC
Read the original article Hit count: 208

Filed under:
|
template <typename Real>
class A{ };

template <typename Real>
class B{ };
//... a few dozen more similar template classes

class Computer{

public slots:
 void setFrom(int from){ from_ = from; }
 void setTo(int to){ to_ = to; }

private:
 template <int F, int T> void compute(){

  using boost::fusion::vector;
  using boost::fusion::at_c;

  vector<A<float>, B<float>, ...> v;
  at_c<from_>(v).operator()(at_c<to_>(v));  //error; needs to be const-expression.
};

This question isn't about Qt, but there is a line of Qt code in my example.

The setFrom() and setTo() are functions that are called based on user selection via the GUI widget. The root of my problem is that 'from' and 'to' are variables. In my compute member function I need to pick a type (A, B, etc.) based on the values of 'from' and 'to'. The only way I know how to do what I need to do is to use switch statements, but that's extremely tedious in my case and I would like to avoid. Is there anyway to convert the error line to a constant-expression?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost