enable_if< is_convertible > w/ explicit constructor trouble

After reading a recent Joel on Software article ( http://www.joelonsoftware.com/items/2003/10/13.html ) and the discussion it generated, I decided to try to write some sort of general strong typedef. I've started out by creating a specific class similar to what would be generated by a `typedef strong_typedef<tag,wrapped_type> name;`, and am trying to use enable_if and is_convertible to allow explicit construction from any type that can be converted to the wrapped type. Unfortunately, I keep getting errors such as the following: main.cpp:36: error: no matching function for call to `position::position(int)' main.cpp:7: error: candidates are: position::position(const position&) main.cpp:14: error: position::position() Compiler version: g++ (GCC) 3.3.5-20050130 (Gentoo Linux 3.3.5.20050130-r1, ssp-3.3.5.20050130-1, pie-8.7.7.1) Here's the cut down code. I'd greatly appreciate it if someone could point me towards what I'm doing wrong. Hopefully I'm just missing something elementary. ( I'm quite new to these boost features. ) On a side note, has anyone come up with a good method of wrapping these rather long SFINAE calls? Thanks, - Scott McMurray // begin code // #include <boost/type_traits.hpp> #include <boost/utility/enable_if.hpp> class position { public: typedef position my_type; typedef unsigned wrapped_type; private: wrapped_type m_data; public: position() {} // This works fine explicit position(boost::enable_if< boost::is_convertible<int, wrapped_type>, int>::type f) : m_data( f ) {} /* // But if I try this, I get the errors template <typename fT> explicit position(typename boost::enable_if< boost::is_convertible<fT, wrapped_type>, fT>::type f) : m_data( f ) {} */ }; int main(int argc, char* argv[]) { position x = position(5); } // end code //

"me22" <me22.ca@gmail.com> wrote
// But if I try this, I get the errors template <typename fT> explicit position(typename boost::enable_if< boost::is_convertible<fT, wrapped_type>, fT>::type f) : m_data( f ) {} */
Try: template <typename fT> explicit position( fT f, typename boost::enable_if< boost::is_convertible<fT, wrapped_type>, void* >::type = 0 ) : m_data( f ) {} regards Andy Little

On 12/05/05, Andy Little <andy@servocomm.freeserve.co.uk> wrote:
Try:
template <typename fT> explicit position( fT f, typename boost::enable_if< boost::is_convertible<fT, wrapped_type>, void* >::type = 0 ) : m_data( f ) {}
Thanks Andy, that worked perfectly. - Scott McMurray

me22 wrote:
After reading a recent Joel on Software article ( http://www.joelonsoftware.com/items/2003/10/13.html ) and the discussion it generated, I decided to try to write some sort of general strong typedef.
The article you linked to doesn't mention typedefs, it's about the alleged harmfulness of exceptions (and it seems poor Joel just didn't understand Resource Acquisition is Initialization in C++ or try-finally in Java/C#). Am I right to assume that with "strong typedef" you mean something like a typedef, but where each identifier defined in this way is an actual type and not just an alias? Best regards, Arne Vogel

On 13/05/05, Arne Vogel <Arne.Vogel@systecs.com> wrote:
The article you linked to doesn't mention typedefs, it's about the alleged harmfulness of exceptions (and it seems poor Joel just didn't understand Resource Acquisition is Initialization in C++ or try-finally in Java/C#).
As quite a number of people are mentioning in his forum =)
Am I right to assume that with "strong typedef" you mean something like a typedef, but where each identifier defined in this way is an actual type and not just an alias?
That's the idea, yes. The idea is to make it so that assigning an unsafe_string to a safe_string ( yes, the names are vague, but they're from the article ) either invalid or cause an applicable transformation function to be called so the resulting safe_string isn't unsafe, despite both being basically just a std::string I started a topic on Joel's forum about it that includes a preliminary, but working, version of the class and an ( oversimplified ) example use case. It's at http://discuss.joelonsoftware.com/default.asp?joel.3.126259.24 if you're interested. I'd also appreciate some design and/or implementation feedback if anyone does check it out. As mentioned, it's my first try at creating a sort of policy-based ( if that's the correct term ) class as well as my first time using boost's SFINAE tools. Regards, Scott McMurray
participants (3)
-
Andy Little
-
Arne Vogel
-
me22