I’m a newbie in C++ and just learned how to use template.
I want to write a Matrix class with the following template arguments:
template < int nRow ,int nCol ,typename T = double >
class ZDMatrix {
public:
ZDMatrix();
ZDMatrix( T arg0 );
ZDMatrix( T arg0 ,T arg1 );
// ... How to declare the constructor with exactly nRow*nCol arguments of type T ??
// ZDMatrix( T arg0 ,T arg1 ,... ) {
// ... number or argN should be nRow*nCol
// }
};
I want to be able to use the matrix like this:
ZDMatrix<2,3> matX( 1 ,1.1 ,1.2 ,2,2.1,2.2 ); // auto convert int 1 to double 1.0
As an alternative, I already try using initializer_list:
ZDMatrix<nRow,nCol,double>::ZDMatrix( std::initializer_list<T> list ) {
assert( list.size() == nRow*nCol );
// ...
}
But the check for number of arguments is at run time not static_assert()
New contributor
Cpp_Newbees is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.