Topic: proposal: explicit return types


Author: acappellaguy@hotmail.com (A Cappella Guy)
Date: Wed, 22 Jan 2003 06:55:52 +0000 (UTC)
Raw View
I've occasionally been in a situation where the design of a class makes
it sensible to vary a method signature by return type only.  An example
would be a string class that supports char and wchar_t strings
interchangeably.  So to support the std string interface, you would
want c_str() able to return two different types.

I propose the keyword "explicit" be usable as a function specifier to
indicate that the return value or reference be usable only in a context
that does not require an implicit cast.  So the following:

class ComboString : public std::wstring {
public:
   // ...
   using std::wstring::c_str;
   explicit const char* c_str() const;
   // ...
};

Would invoke the const char* version of c_str() iff it is used without
cast, a la:

const char foo* = myComboString.c_str();

Any other use of the c_str() method would default to the non-explicit
std::wstring version or fail to compile if no match is made.

Useful?  Implementable?  Complications?

 -- A Cappella Guy

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]