template <class T>
T* create_class(T* object = nullptr)
{
if (obj == nullptr) {
obj = new T();
if (obj == nullptr)
throw Exception("Can't create " + std::string(name_of<T>()) + " object");
}
obj->init();
return obj;
}
...
MyClass* mc = create_class<MyClass>();
delete mc;
void *p = nullptr;
if (!p) {
throw "Не вызовется";
}
#include <iostream>
using namespace std;
template<typename DataType>
struct Data {
static const DataType m_default_value;
DataType m_data;
Data() : m_data(m_default_value) {}
Data(const DataType& value) : m_data(value) {}
operator DataType () const {
return m_data;
}
void operator = (const DataType& _data) {
m_data = _data;
}
};
template <> Data<double>::m_default_value = 0.;
int main(int argc, char** argv)
{
Data<double> data = 3.14159;
cout << data << endl;
return EXIT_SUCCESS;
}
#include <vector>
using namespace std;
void foo(vector<ArrayData>& data)
{
for (vector<ArrayData>::iterator it = data.begin(); it != data.end(); ++it)
cout << it->data[0] << " ";
cout << endl;
}
int main() {
vector<ArrayData> test;
ArrayData data0 = {{1, 2, 3}, 3};
test.push_back(data0);
foo(test);
}