Still fighting with templates. In this example, despite the fact that is copied straight from a book I’m getting the following error message: Error 2 error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could not deduce template argument for 'int C::* ' from 'int'.
This is an example from a book Templates — The Complete Guide.
(I work with Visual Studio 2010 RC).
template<typename T>
class IsClassT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(…);
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};
class MyClass {
};
struct MyStruct {
};
union MyUnion {
};
void myfunc()
{
}
enum E {e1} e;
// check by passing type as template argument
template <typename T>
void check()
{
if (IsClassT<T>::Yes) {
std::cout << " IsClassT " << std::endl;
}
else {
std::cout << " !IsClassT " << std::endl;
}
}
// check by passing type as function call argument
template <typename T>
void checkT (T)
{
check<T>();
}
int main()
{
/*std::cout << "int: ";
check<int>(); */
std::cout << "MyClass: ";
check<MyClass>();
}
And although I know roughly what’s going on in this example I cannot fix this error.
Thanks for help.
Bill
14.1k4 gold badges42 silver badges54 bronze badges
asked Mar 16, 2010 at 13:50
10
My compiler (MSVC2008TS) likes it if you don’t fully qualify the test
expression:
enum { Yes = sizeof(test<T>(0)) == 1 };
But is this even legal code?
answered Mar 16, 2010 at 14:23
John DiblingJohn Dibling
98.8k29 gold badges183 silver badges321 bronze badges
0
Shouldn’t this line
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
be
enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1 };
instead?`
(And because I’m anal, I’d write sizeof(IsClassT<T>::template test<T>(0)) == sizeof(One)
.)
answered Mar 16, 2010 at 15:16
5
Not exactly an answer to this question, but rather a different approach to your problem. I find it easier to do SFINAE tests in specializations rather than in functions defined in that template:
// used to pass a type tested with SFINAE
template<typename T> struct tovoid { typedef void type; };
Using it to pass a type that may be invalid:
template<typename T, typename U = void>
struct is_class {
static bool const value = false;
};
// if "int T::*" is a valid type, this specialization is used
template<typename T>
struct is_class<T, typename tovoid<int T::*>::type> {
static bool const value = true;
};
This way it’s considerably shorter and the noise with sizeof
and things aren’t done.
answered Mar 17, 2010 at 7:38
- Remove From My Forums
-
Question
-
I looked the similar posted (and often answered) issues. It seems like this problem may related to the new C+0x standard. However, I need to figure out a way to build it. So any workaround, or the right way would be appreciated
The error message:
error C2784: ‘Vec<D, T> round(const Vec<D,T> &)’: cannot deduce const Vec <D,T> &’ template argument from ‘float’
#define VEC_DECLARE_ONEARG(name) template <int D, class T> static inline Vec<D,T> name(const Vec<D,T> &v) { ... } VEC_DECLARE_ONEARG(round) template <class T> static inline T clamp(const T &x, const T &a, const T &b) { ... } clamp(int(round(n->col[0] * 255.0f)), 0, 255)
-
Edited by
Tuesday, November 9, 2010 3:32 AM
Typo corrected
-
Edited by
Answers
-
There is no surer way whether VC6 has the float round(float) than testing with installed VC6. But don’t you have other versions of Visual Studio installed? Would those higher version of Visual Studio installation supersede the definition of math.h?
Does VCs keep their own math.h?I think there are three possible conclusions then
1. VCs have never supported round, at least from the VC6.
2. If higher version of VC installed then math.h is replaced accordingly so we can be absolutely sure about round() if we install VC6 before higher version of a VC is installed.
3. The author used mingw or something like that on Windows in order to build the source with more C99 standard friendly compiler.
Regardless the version of VC, I think so far we can say that the Vec<D,T> round(const Vec<D,T>) was never intended when ’round(n->col[0] … )’ called but ‘float round(float)’ defined in math.h.
In short, a simple program as below will not compile with VCs but gcc. (Tested with VC10 and gcc 4.1)
#include <iostream> #include <math.h> using namespace std; int main() { cout << round(1.5f) << endl; }
I have found another MSDN thread about VC8.0 (so I think so forth) does not fully support the C99 standard since they focus on C++ standard.
http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/913a2001-d2a6-450a-a15e-ff97ab6da0f1/
There is some consistent statement in a Wikipedia page about C99 standard.
«there has been less support from vendors such as Microsot and Borland that have mainly focused on C++» (en.wikipedia.org/wiki/C99, fetched on 13 Nov 2010)
It would be better and more authoritative if I could have found a list of supported functions of C99 standard published by Visual Studio team. But my quick web search didn’t get to that.
If those statements are correct, then we can say that VCs are not fully up to C99 standards. As we know at least now is that ‘float round(float)’ is one of those not supported.
Lee Seongjoo
Ph.D. Candidate
Computer Science
Yonsei Univerisity
Seoul, Korea-
Marked as answer by
Lee Seongjoo
Saturday, November 13, 2010 2:04 AM -
Edited by
Lee Seongjoo
Saturday, November 13, 2010 2:06 AM
-
Marked as answer by
-
lseongjoo wrote:
There is no surer way whether VC6 has the float round(float) than testing with installed VC6. But don’t you have other versions
of Visual Studio installed?I do.
Would those higher version of Visual Studio installation supersede the definition of math.h? Does VCs
keep their own math.h?Each version comes with its own set of headers and libraries. You could configure one to use the other’s, I suppose, but it would be unwise.
In any case, no VC version comes with round() function in math.h, to my knowledge.
1. VCs have never supported round, at least from the VC6.
I believe this to be the case.
2. If higher version of VC installed then math.h is replaced accordingly
This absolutely doesn’t happen.
3. The author used mingw or something like that on Windows in order to build the source with more C99 standard friendly compiler.
Quite possible.
Igor Tandetnik
-
Marked as answer by
Lee Seongjoo
Saturday, November 13, 2010 2:05 AM
-
Marked as answer by
AliceO 0 / 0 / 1 Регистрация: 08.11.2014 Сообщений: 6 |
||||
1 |
||||
18.11.2014, 10:19. Показов 1650. Ответов 2 Метки нет (Все метки)
имеется шаблонный список
error C2784: ‘bool std::operator !=(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)’ : could not deduce template argument for ‘const std::istreambuf_iterator<_Elem,_Traits> &’ from ‘list<TYPE>::iterator’
__________________
0 |
Вездепух 10435 / 5704 / 1553 Регистрация: 18.10.2014 Сообщений: 14,098 |
|
18.11.2014, 10:26 |
2 |
could not deduce template argument Ну так а оператор ‘!=’ вы для своего итератора определили? Не определили. Компилятор в отчаянии пытается приспособить какой-то другой оператор ‘!=’, но у него не получается. Поэтому ошибка. Вперед определять операторы ‘==’ и ‘!=’ для вашего итератора.
0 |
AliceO 0 / 0 / 1 Регистрация: 08.11.2014 Сообщений: 6 |
||||
18.11.2014, 18:05 [ТС] |
3 |
|||
Спасибо за ответ.
и fatal error C1001: An internal error has occurred in the compiler Добавлено через 16 минут
0 |
Home » Programming » C++ Error C2784 – Working with GUID keys in map data structures
Error C2784 is caused by problems with the compiler for using a key in map data structures. Map data structures are very useful in C++, their C# equivalent being the Dictionary structure. They allow you to store data to a key, which means when you come to need that data you can use the key, rather than iterating through a vector or similar.
Error C2784 – Could not deduce template argument
C++ Error C2784
What is it? | Error C2784 highlights that the key you are using in your map structure cannot deduce a template argument from a lack of a ‘<‘ operator. |
Common causes | C2784 can be caused by any key data type which does not contain an overloaded ‘<‘ operator. Keys are sorted by the map structure, however, if there is no ‘<‘ operator to perform comparisons with, the keys cannot be sorted and, as such, C++ will not compile until it can. |
How to fix it? | This error can be fixed by providing an overload for the ‘<‘ operator yourself. The example below shows how to deal with the GUID data structure, which is a common key in maps due to the unique nature of GUIDs (i.e. no two GUIDs will be the same allowing for unique key access). The example given below can be adapted for any data structure which does not contain the necessary ‘<‘ operator. |
Example fix
The following code is for using GUIDs as keys, but can be adapted as necessary for any data type which needs to fix this error. Commonly, you would put any overloaded operators in the class in which they relate (i.e. if you created your own co-ordinate class and needed to overload the ‘<‘ operator you would do so in that class) however, this may not always be possible particularly if you are overloading it for a class you have not made. I myself use a common tools file in which I contain some common methods which may be needed across several classes, which is where I place this fix for GUID key mapping.
inline bool operator< (const GUID &firstGUID, const GUID &secondGUID) { return (memcmp(&firstGUID, &secondGUID, sizeof(GUID)) < 0 ? true : false); }
About Author
FraserG
Computer Scientist currently undertaking an Engineering Doctorate degree discussing computing, programming, research and racing.
- Forum
- General C++ Programming
- Error C2784 — Please Help!
Error C2784 — Please Help!
Hi there, I’m new to this forum and pretty new to c++ as well and I can’t seem to get this error figured out. Here is the full error message:
C2784: ‘std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)’ : could not deduce template argument for ‘std::basic_istream<_Elem,_Traits> &&’ from ‘std::ostream’
Here is my C++ code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string name;
double roomcharge,extracharge,total;
double roomnum,numofdays;
const double roomrate=125;
cout << fixed;
cout << «Please fill in the following information to output the Total room charges, and an invouce for the guest: » << endl << endl;
cout << «Customer Name: «;
getline(cin, name);
cout << «Room Number: «;
cin >> roomnum;
cout << «Number of Days Stayed: «;
cin >> numofdays;
cout << «Extra Charges: «;
cin >> extracharge;
roomcharge=roomrate*numofdays;
total=roomcharge+extracharge;
cout << «Star Hotel Customer Invoice: «;
cout << «Customer Name: «;
system(«pause»);
}
My Project is still incomplete at the moment, but I don’t want to continue until I get this error fixed.
Any help would be appreciated, thanks in advance.
Since you don’t have something like cout >> name ;
anywhere in your code, you should not be getting this error.
Hm, well that’s really weird because it’s still giving me issues. I think I’ll start a new project in Visual Studio and re-enter the code. Thanks for your input.
I’ll post my results here after trying a new project.
I expect the code you think you’re compiling is not the code you are compiling.
Moschops you may be right. I made a new project and just pasted the same code in, and it built successfully. No idea how or why, but it’s fine now. Thanks for all of the help
I didn’t think I had any errors in my code >.> <.<
Topic archived. No new replies allowed.
Got the above error, when compiling something that looked like this:
std::map<std::string, myType> myMap;
myMap[“Test”] = myType();
Looked around on the net, didn’t find the answer, thought about it for a bit and then realised I was missing #include <string> which fixed the problem. The error was a bit cryptic. Look for yourself at this dump from the compiler output:
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘
1> C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(142) : while compiling class template member function ‘bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const’
1> with
1> [
1> _Ty=std::string
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludemap(72) : see reference to class template instantiation ‘std::less<_Ty>’ being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(26) : see reference to class template instantiation ‘std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>’ being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=std::string,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,std::string>>,
1> _Mfl=false
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(68) : see reference to class template instantiation ‘std::_Tree_nod<_Traits>’ being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(94) : see reference to class template instantiation ‘std::_Tree_ptr<_Traits>’ being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(112) : see reference to class template instantiation ‘std::_Tree_val<_Traits>’ being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> C:Program FilesMicrosoft Visual Studio 8VCincludemap(82) : see reference to class template instantiation ‘std::_Tree<_Traits>’ being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> c:projectsflewsesourcex-planeflewseVisualSettings.h(16) : see reference to class template instantiation ‘std::map<_Kty,_Ty>’ being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=std::string
1> ]
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludextree(1372) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludevector(1276) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludexutility(1880) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:Program FilesMicrosoft Visual Studio 8VCincludeutility(76) : see declaration of ‘std::operator <‘
1>C:Program FilesMicrosoft Visual Studio 8VCincludefunctional(143) : error C2676: binary ‘<‘ : ‘const std::string’ does not define this operator or a conversion to a type acceptable to the predefined operator
I am very new to object oriented programming and C++. I have been working on a matrix class and squarematrix class and have been running into some problems that I can’t seem to figure out. The error code I have been getting is:
C2784:
'matrix<T,m,k> operator *(matrix<T,m,n> &,matrix<T,n,k> &)': could not
deduce template argument for 'matrix<T,m,n> &' from
'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'
I am really unsure why, because I have had other parts of my code work. The error is reported in the line with «product.elements = product.elements * elements;»
//Source.cpp
#include"Squarematrix.h"
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<double> a = { 1, 2,4,5,6};
squarematrix<double,2> N;
N.assign(a);
cout << N << N.pow(2)<< endl;
return(0);
}
//Matrix.h
#ifndef _Matrix_
#define _Matrix_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
template<class T, int m, int n>
class matrix {
public:
vector<vector<T>> elements;
int nrow;
int ncol;
matrix();
matrix(matrix<T, m, n>&);
};
template<class T, int m, int n>
matrix<T, m, n>::matrix() {
vector<T>temp(n, 0);
elements.assign(m, temp);
nrow = m; //m=0
ncol = n; //n=0
}
template<class T, int m, int n>
matrix<T, m, n>::matrix(matrix<T, m, n>& a) {
elements = a.elements;
nrow = m;
ncol = n;
}
template<class T, int m, int n, int k>
matrix<T, m, k> operator*(const matrix<T, m, n>& a, const matrix<T, n, k>& b) {
matrix<T, m, k> product;
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
for (int h = 0; h < n; h++)
product.elements[i][j] += a.elements[i][h] * b.elements[h][j];
}
}
return product;
}
template<class T, int m, int n>
ostream& operator<< (ostream& o, const matrix<T, m, n>& input) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j)
o << input.elements[i][j] << " ";
o << endl;
}
return o;
}
#endif _Matrix_
//Squarematrix.h
#ifndef _Squarematrix_
#define _Squarematrix_
#include "Matrix.h"
#include <iostream>
#include <vector>
using namespace std;
template<class T, int n>
class squarematrix : public matrix<T, n, n> {
public:
squarematrix();
squarematrix(squarematrix<T, n>&);
squarematrix<T, n> pow(int); //calculate A^k
};
template<class T, int n>
squarematrix<T, n>::squarematrix(){
vector<T>temp(n, 0);
elements.assign(n, temp);
nrow = n; //n=0
ncol = n; //n=0
}
template<class T, int n>
squarematrix<T, n>::squarematrix(squarematrix<T, n>& a){
elements = a.elements;
nrow = n;
ncol = n;
}
template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
squarematrix<T, n> product;
product.elements = elements;
for (int power = 2; power <= k; power++) {
product.elements = product.elements * elements;
}
return product;
}
#endif _Squarematrix_
You don’t need nrow
and ncol
— they’re template parameters and known at compile time.
But that’s not the problem — you’re multiplying std::vector
where you should be multiplying squarematrix
:
template<class T, int n>
squarematrix<T, n> squarematrix<T, n>::pow(int k){
squarematrix<T, n> product = unit_matrix<T, n>();
for (int power = 1; power <= k; power++) {
product = product * *this;
}
return product;
}
where I’ve used a fictitious function that creates a unit matrix.
Writing that function left as an exercise.