I have this code
template <typename T> class KeyValueProperty { protected: T value = T(); std::string key = ""; public: KeyValueProperty(const std::string & key) : key(key) { } T & operator = (const T &i) { return value = i; }; operator const T & (){ return value; }; }; struct T2 { KeyValueProperty<std::string> x {"x"}; KeyValueProperty<double> y {"y"}; }; and in main
T2 tx; tx.x = "hellow"; tx.y = 10; std::cout << static_cast<std::string>(tx.x) << ::std::endl; std::cout << tx.y << ::std::endl; This is working correctly. However, doing only this
std::cout << tx.x << ::std::endl; will end up in
error C2679: binary '<<': no operator found which takes a right-hand operand of type 'Test::KeyValueProperty' (or there is no acceptable conversion)
Is it possible to have auto-conversion, or I must manually call casting?