Здравствуйте, Oleg A. Bachin, Вы писали:
//Заглушка чтобы компилятор не орал.
struct null_value_compare
{
template<class T>
null_value_compare(T const&)
{}
};
template <typename T>
struct nullable
{
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef value_type const* const_pointer;
typedef value_type const& const_reference;
nullable()
: is_null_(true)
{}//; после функций не надо.
nullable(const_reference other)
: is_null_(false)
, value_(other)
{}
bool is_null() const //возвращат ссылку не надо bool легкий тип.
//Болие того оно даже не скомпилится ибо возвращать не константную ссылку на поле объекта из константного метода...
{
return is_null_;
}
friend bool operator==(nullable const& lhs, nullable const& rhs)
{
if (lhs.is_null() || rhs.is_null()) //функции надо вызывать :))
throw null_value_compare("null_value_compare");
return lhs.value_==rhs.value_;
}
pointer operator->()
{
return &value_;
}
const_pointer operator->()const
{
return &value_;
}
private:
value_type value_;
bool is_null_;
};
int main()
{
try
{
nullable<std::string> str1 = "std::string";
nullable<std::string> str2 = "asd";
nullable<std::string> str3;
std::cout << str1->c_str() << std::endl;
std::cout << (str1==str1) << std::endl;
std::cout << (str1==str2) << std::endl;
std::cout << (str2==str2) << std::endl;
std::cout << (str1==str3) << std::endl;
}
catch(...)
{
std::cout<<"oops\n";
}
}
... << RSDN@Home 1.1.3 beta 1 >>