I'm learning C++. I have a Classroom class which should behave one way or another depending on the Configuration object that is being used. I could pass that Configuration object in the constructor when creating the Classroom object like this:
class Classroom {
private:
Configuration conf;
public:
Classroom(Configuration conf_){
conf = conf_;
}
/** more member functions that use conf **/
};
But I thought it would be cooler if I could use a template for it. The Configuration object would be passed as template argument when creating the Classroom object. This is what I came up with, but it doesn't work:
template<Configuration &conf>
class Classroom {
int doSomething(int n){
// member function that uses data in Configuration object
return n + conf.config_1;
}
};
struct Configuration {
public:
int config_1;
};
int main() {
Configuration conf;
conf.config_1 = 95;
Classroom<conf> myClassroom;// doesn't work
}
It says: error: the value of 'conf' is not usable in a constant expression.
What am I missing?