Suppose I have a class:
class A {
protected:
vector<double> p1;
vector<double> p2;
...
vector<double> pn;
public:
//Constructors and destructor
void SetP1( int, double );
void SetP2( int, double );
...
void SetPn( int, double );
};
Where the structure of all setter's definitions is the same. I am not glad to see this copy-paste here. Is there a C++
's style to get rid of this?
class A {
public:
enum {count = 55};
protected:
std::array<std::vector<double>, count> p;
public:
//Constructors and destructor
template<std::size_t N>
void SetP( std::size_t x, double d ) {
static_assert( N<count, "out of bounds" );
// I assume we want auto-resize:
if (x >= p[N].size()) p[N].resize(x+1);
p[N][x] = d;
}
};
Use:
A a;
a.SetP<22>( 7, 3.14 );