I'm trying to learn how to use custom C++ exceptions with template arguments. This is a dummy program that I'm trying to compile unsuccessfully:
#include <string>
#include <iostream>
template <class T>
class MyException : public std::exception {
public:
T error;
MyException(T err) { error = err; };
};
int main(void) {
try {
std::string err = "String error";
throw MyException<std::string>(err);
return 0;
}
catch (MyException e) {
std::cout << e << std::endl;
return 1;
};
}
This is the error I get:
<source>: In function 'int main()':
<source>:18:9: error: invalid use of template-name 'MyException' without an argument list
18 | catch (MyException e) {
| ^~~~~~~~~~~
<source>:18:9: note: class template argument deduction is only available with '-std=c++17' or '-std=gnu++17'
<source>:5:7: note: 'template<class T> class MyException' declared here
5 | class MyException : public std::exception {
| ^~~~~~~~~~~
<source>:19:22: error: 'e' was not declared in this scope
19 | std::cout << e << std::endl;
| ^
Could you please help me to fix it for C++11?
Aucun commentaire:
Enregistrer un commentaire