I am having an interesting problem in C++ that goes back to declarations of functions in templated classes that become identical for certain choices of template parameters.
As a simple example, consider a templated class "Property" that shall support both cast to std::string and cast to the template type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <typename Type> class Property {
protected:
Type data;
public:
/// Implicit conversion to template type.
operator Type() const { return data; }
/// Implicit conversion to std::string.
operator std::string() const { return toString(data); }
};
// VS: error C2535: 'Property<Type>::operator Type(void) const' :
// member function already defined or declared
Property<std::string> blubb;
I'm curious how to get around that issue!
For smartasses: my question was not if this is good style. It's an example demonstrating my issue.
You can't get around that issue. If 'Type' is std::string then you have operator std::string() const twice and the compiler doesn't know which one to choose.
It works thanks to the forward déclaration. In the second class template, Type means in fact everything except std::string beacause of the first specialisation.
darkestfright, i don't think your code is what the author of the topic wants because it does not defines a string conversion for Property<T> where T is not a string.