Why doesn't the compiler throw an error if I don't initialize const variables in an unused template class? If I remove the template keyword the compiler complains as expected.
This code works (unexpected):
#include <iostream>
template<class T>
class Matrix {
private:
const uint8_t numRows, numColumns;
T content[];
public:
Matrix(uint8_t numRows, uint8_t numColumns)
// : numRows(numRows), numColumns(numColumns) -- Works fine
{
std::cout << "Matrix" << std::endl;
}
};
int main() {
// If Matrix constructor is not called the compiler doesn't need a initialization list
// Matrix<int> matrix(2, 2);
return 0;
}
But this one does not (expected):
#include <iostream>
template<class T>
class Matrix {
private:
const uint8_t numRows, numColumns;
T content[];
public:
Matrix(uint8_t numRows, uint8_t numColumns)
// : numRows(numRows), numColumns(numColumns) -- Does NOT work
{
std::cout << "Matrix" << std::endl;
}
};
int main() {
Matrix<int> matrix(2, 2);
return 0;
}