[type_traits] copy constructor

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[type_traits] copy constructor

lcaminiti
Hello all,

What's the most portable way to detect if a type T is CopyConstructible?

Actually, I would need a metafunction that is true_ iff a type T has a
const-correct copy construct:

T::T( T const& other) { /* copy this T from the other T */ }

(I've been looking at boost::has_trivial_copy and
boost::has_nothrow_copy but I wanted to double check with the ML.)

Thanks a lot.
--Lorenzo

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

John Maddock-3
> What's the most portable way to detect if a type T is CopyConstructible?
>
> Actually, I would need a metafunction that is true_ iff a type T has a
> const-correct copy construct:
>
> T::T( T const& other) { /* copy this T from the other T */ }
>
> (I've been looking at boost::has_trivial_copy and
> boost::has_nothrow_copy but I wanted to double check with the ML.)

There's no trait for that because we could think of no way of implementing
it: it would basically require a "does this code compile" compiler intrinsic
which vendors have been very reluctant to provide (read that as v. difficult
for them to implement).

HTH, John.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

Vicente Botet
In reply to this post by lcaminiti
Le 29/09/11 04:57, Lorenzo Caminiti a écrit :

> Hello all,
>
> What's the most portable way to detect if a type T is CopyConstructible?
>
> Actually, I would need a metafunction that is true_ iff a type T has a
> const-correct copy construct:
>
> T::T( T const&  other) { /* copy this T from the other T */ }
>
> (I've been looking at boost::has_trivial_copy and
> boost::has_nothrow_copy but I wanted to double check with the ML.)
>
>
Hi,

The proposed Boost.Conversion provides some interesting traits (included
in C++11) that you could be interested in.
is_constructible, is_default_constructible, is_copy_constructible,
is_move_constructible as well as is_assignable, is_copy_assignable and
is_move_assignable.

If John is interested in we can work together to include them in TypeTraits.

Best,
Vicente

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

lcaminiti
Vicente Botet wrote
Le 29/09/11 04:57, Lorenzo Caminiti a écrit :
> Hello all,
>
> What's the most portable way to detect if a type T is CopyConstructible?
>
> Actually, I would need a metafunction that is true_ iff a type T has a
> const-correct copy construct:
>
> T::T( T const&  other) { /* copy this T from the other T */ }
>
> (I've been looking at boost::has_trivial_copy and
> boost::has_nothrow_copy but I wanted to double check with the ML.)
>
>
Hi,

The proposed Boost.Conversion provides some interesting traits (included
in C++11) that you could be interested in.
is_constructible, is_default_constructible, is_copy_constructible,
is_move_constructible as well as is_assignable, is_copy_assignable and
is_move_assignable.

If John is interested in we can work together to include them in TypeTraits.
Thanks a lot Vicente. For now, I'm sticking with C++03 but I have a list of extra features that Boost.Contract could support with C++11 for future work. I'll add this (important) feature to the list.

--Lorenzo
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

Vicente Botet
Le 29/09/11 19:12, lcaminiti a écrit :

> Vicente Botet wrote:
>> Le 29/09/11 04:57, Lorenzo Caminiti a écrit :
>>> Hello all,
>>>
>>> What's the most portable way to detect if a type T is CopyConstructible?
>>>
>>> Actually, I would need a metafunction that is true_ iff a type T has a
>>> const-correct copy construct:
>>>
>>> T::T( T const&   other) { /* copy this T from the other T */ }
>>>
>>> (I've been looking at boost::has_trivial_copy and
>>> boost::has_nothrow_copy but I wanted to double check with the ML.)
>>>
>>>
>> Hi,
>>
>> The proposed Boost.Conversion provides some interesting traits (included
>> in C++11) that you could be interested in.
>> is_constructible, is_default_constructible, is_copy_constructible,
>> is_move_constructible as well as is_assignable, is_copy_assignable and
>> is_move_assignable.
>>
>> If John is interested in we can work together to include them in
>> TypeTraits.
>>
> Thanks a lot Vicente. For now, I'm sticking with C++03 but I have a list of
> extra features that Boost.Contract could support with C++11 for future work.
> I'll add this (important) feature to the list.
>
The definition of these traits relies on SFINAE for expressions which is
an optional feature for C++03 and mandatory for C++11.

Unfortunately this feature is not supported most of C++03 compilers and
even msvc 10 which supports some c++11 features doesn't support it.

Best,
Vicente
Best,
Vicente


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

John Maddock-3
In reply to this post by lcaminiti
>> The proposed Boost.Conversion provides some interesting traits (included
>> in C++11) that you could be interested in.
>> is_constructible, is_default_constructible, is_copy_constructible,
>> is_move_constructible as well as is_assignable, is_copy_assignable and
>> is_move_assignable.
>>
>> If John is interested in we can work together to include them in
>> TypeTraits.

For sure, care to provide a submission/patch?

Thanks, John.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [type_traits] copy constructor

lcaminiti
John Maddock-3 wrote
>> The proposed Boost.Conversion provides some interesting traits (included
>> in C++11) that you could be interested in.
>> is_constructible, is_default_constructible, is_copy_constructible,
>> is_move_constructible as well as is_assignable, is_copy_assignable and
>> is_move_assignable.
>>
>> If John is interested in we can work together to include them in
>> TypeTraits.

For sure, care to provide a submission/patch?

Thanks, John.
If these (IMO very useful) traits are added, I'd suggest to have macros #defined when the traits can actually do the job-- same as BOOST_HAS_TRIVIAL_COPY, etc.

For example, I'd use it this way:

template< typename T >
struct has_oldof :
#if defined(BOOST_HAS_COPY_CONSTRUCTOR)
    boost::has_copy_constructor<T> // from Boost.Conversion's is_copy_constructible
#else
    boost::mpl::true_ // user specializes has_oldof when can't copy T :(
#endif
{};

Thanks,
--Lorenzo