Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between is_constructible and is_default_constructible

21 views
Skip to first unread message

Mut...@dastardlyhq.com

unread,
Jan 2, 2023, 4:45:08 AM1/2/23
to
Morning

What exactly is the difference between these 2 meta functions? I always
thought is_constructible was true if any contructor existed and
is_default_constructible only if the default constructor existed. But with
clang it seems they are identical so if I delete the default constructor
then is_constructible returns false as well. Eg:

#include <iostream>
#include <type_traits>

using namespace std;

struct S
{
S() = delete;
S(int) { }
S(const S &) { }
};

int main()
{
if (is_constructible<S>::value)
cout << "constructible\n";
if (is_default_constructible<S>::value)
cout << "default constructible\n";
if (is_copy_constructible<S>::value)
cout << "copy construtible\n";
}

The above will only print "copy constructible". What have I missed?

Bonita Montero

unread,
Jan 2, 2023, 5:06:25 AM1/2/23
to
is_constructible checks if a class is constructible
with the given variadic parameters of is_constructible.

Mut...@dastardlyhq.com

unread,
Jan 2, 2023, 5:09:51 AM1/2/23
to
Hmm. They should have picked a different name then IMO. Thanks.

Bonita Montero

unread,
Jan 2, 2023, 5:31:22 AM1/2/23
to
Am 02.01.2023 um 11:09 schrieb Mut...@dastardlyhq.com:

> Hmm. They should have picked a different name then IMO. Thanks.

I felt irritated about that only five seconds an then I thought
that there must be variadic parameters on is_constructible.

Bo Persson

unread,
Jan 2, 2023, 5:47:28 AM1/2/23
to
is_constructible looks for a specific constructor, matching its
parameters, so is_constructible<S, int> will be true.

If you use just is_constructible<S> that *is* the same as
is_default_constructible.


Bonita Montero

unread,
Jan 2, 2023, 10:08:44 AM1/2/23
to
Am 02.01.2023 um 10:44 schrieb Mut...@dastardlyhq.com:
I'm asking myself whether there's a is_default_constructible.
is_constructible with no additional parameters reports the
same value.

Frederick Virchanza Gotham

unread,
Jan 19, 2023, 11:23:28 AM1/19/23
to
On Monday, January 2, 2023 at 10:47:28 AM UTC, Bo Persson wrote:

> is_constructible looks for a specific constructor, matching its
> parameters, so is_constructible<S, int> will be true.
>
> If you use just is_constructible<S> that *is* the same as
> is_default_constructible.


Instead of using "is_constructible_v<MyClass,MyOtherClass>", an alternative would be:

#include <utility> // declval

if constexpr ( requires { MyClass( std::declval<MyOtherClass>() ) } ) DoSomething();


0 new messages