Constant value nicht als Template Argument akzeptiert

Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.

Constant value nicht als Template Argument akzeptiert
Hallo,

ich habe folgendes Problem. Und zwar krieg ich bei folgendem Codeabschnitt mit Bezug auf numGridPoints bei der Instantiierung von Matrix A die Fehlermeldung “expression must have a constant value”:

void testFullMatrix (const int numGridPoints) {
	const double hx = 1. / (numGridPoints - 1);
	const double hxSq = hx * hx;
	std::cout << "Starting full matrix solver for " << numGridPoints << " grid points with normal Matrix class" << std::endl;

	Matrix<double, numGridPoints, numGridPoints> A();

wobei der dazugehörige Matrix Konstruktor folgendermaßen aussschaut:

template<typename T, size_t rows, size_t cols>
class Matrix: public MatrixLike<T,Matrix<T,size_t rows, size_t cols>, size_t rows, size_t cols>{
public:
	Matrix(T value = 0.) : data_(new T[rows*cols]) {
		for (unsigned int i = 0; i < rows*cols; ++i) {
			data_[i] = value;
		}
	}

Ich meine zu glauben, dass das Problem ist, dass ich eine constant expression brauche, damit ich zur Compilezeit weiß was in numGridPoints steht. Meine Frage ist also, vorausgesetzt meine Annahme stimmt, wie bekomme ich aus numGridPoints eine constexpr, die ich als Templateargument verwenden kann?

Ich danke schonmal für jegliche Hilfe =)


template <int numGridPoints>
void testFullMatrix () {
	const double hx = 1. / (numGridPoints - 1);
	const double hxSq = hx * hx;
	std::cout << "Starting full matrix solver for " << numGridPoints << " grid points with normal Matrix class" << std::endl;

	Matrix<double, numGridPoints, numGridPoints> A();

oder nicht.


Danke dir, das wars. Macht natürlich Sinn, dass ich die Funktion auch templaten muss damits der Compiler sieht ^^