Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
user defined exception
//clang 3.8.0 #include <string> #include <exception> #include <iostream> class MyException:public std::exception{ std::string m_error; public: MyException(std::string error):m_error(error){} virtual const char* what()const noexcept{ return m_error.c_str(); } }; template <class T> class Array4{ T *m_data=new T[4]; int m_index; public: T& operator[](int index){ m_index=index; if(index>=4) throw MyException("invalid memory reference"); return m_data[index]; } friend std::ostream& operator<<(std::ostream& out,Array4 arr ){ out<<arr[arr.m_index]; return out; } ~Array4(){delete[] m_data;} }; int main() { try{ Array4<std::string> arr; arr[4]="booom"; std::cout<<arr[4]; }catch(std::exception& e){ std::cerr<<"Error: "<<e.what(); }; }
run
|
edit
|
history
|
help
0
for_each_argument
non-template template parameters for container stream insertion: SFINAE
problem_name_1
ArrayList
simple in-memory b-tree
Variable declarations in while loop conditions are also C++-only.
Exploring stringstreams
return reference (clang)
tuple, order of members
list iota cout