Boost logo

Boost :

Subject: Re: [boost] [utility/value_init] boost::value_initialized<T> direct-initialized?
From: Niels Dekker - address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2010-04-02 04:41:57


Jeffrey Lee Hellrung, Jr. wrote:
> What do you think about using the templated constructor *and* SFINAE out
> via enable_if the binding of U to initialized<T> (or, maybe, to any
> class derived from initialized<T>)?

SFINAE/enable_if might be interesting indeed, but I'm not sure if it's
really necessary. And I don't think it will help to work around the
specific MSVC bug
<http://connect.microsoft.com/VisualStudio/feedback/details/423737>.

Here is an example that fails to compile on MSVC because of the compiler
bug. Please let me know if you can fix it by SFINAE. (But I'm afraid it
won't work.)

   //////////////////////////////////////////////////
   namespace boost {
     template<class T> class initialized
     {
       T m_data;
     public:
       initialized()
       : m_data()
       {
       }

       initialized(const initialized& arg)
       : m_data(arg.m_data)
       {
       }

       template<class U>
       explicit initialized(const U& arg)
       : m_data(arg)
       {
       }

       operator T&()
       {
         return m_data;
       }

       operator const T&() const
       {
         return m_data;
       }
     };
   }

   //////////////////////////////////////////////////
   // USER CODE STARTS HERE

   class my_initialized_int: boost::initialized<int>
   {
   };

   int main()
   {
     my_initialized_int i1;
     my_initialized_int i2 = i1;
   }
   //////////////////////////////////////////////////

The example above compiles well on Comeau 4.3.10.1
<www.comeaucomputing.com/tryitout> and GCC 4.1.2
<http://codepad.org/NCZKJ3OE>. But it fails on MSVC, as
www.dinkumware.com/exam says:

   error C2247: 'boost::initialized<T>::operator const int &' not
accessible because 'my_initialized_int' uses 'private' to inherit from
'boost::initialized<T>'

Would boost::initialized<T> really need to have a /templated/
constructor, initialized(const U&)? Wouldn't a non-templated
initialized(const T&) be sufficient, for initialized<T>? Note that
that's just what Edward was asking for
<https://svn.boost.org/trac/boost/ticket/3472>. Do you have a motivating
use case for a templated constructor of initialized<T>?

Anyway, I /do/ think that it would be useful to add move semantics, by
adding a initialized(T&&) constructor, for those compilers that support
rvalue references.

Kind regards,

   Niels

-- 
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk