Why doesn't this use of std::is_constructible compile?
Asked Answered
I

1

9

One of my container's constructors default-constructs an allocator as a default parameter value:

template<class T, class Allocator>
struct my_container
{
  my_container(int n, Allocator alloc = Allocator()) {}
};

Presumably, this constructor is enabled only when Allocator can be default-constructed.

I would like to test, with std::is_constructible, whether this constructor may be used with an allocator which is not default constructible:

template<class T>
struct my_not_default_constructible_allocator
{
  // no default ctor
  my_not_default_constructible_allocator(int) {}
};

However, when I apply std::is_constructible, I get a compile-time error, rather than false, which is what I expect:

#include <type_traits>

template<class T, class Allocator>
struct my_container
{
  my_container(int n, Allocator alloc = Allocator()) {}
};

template<class T>
struct my_not_default_constructible_allocator
{
  // no default ctor
  my_not_default_constructible_allocator(int) {}
};

int main()
{
  bool result = std::is_constructible<my_container<int, my_not_default_constructible_allocator<int>>, int>::value;

  return 0;
}

Compiler output:

$ clang -std=c++14 repro.cpp 
repro.cpp:6:41: error: no matching constructor for initialization of 'my_not_default_constructible_allocator<int>'
  my_container(int n, Allocator alloc = Allocator()) {}
                                        ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:976:24: note: in instantiation of default function argument expression for
      'my_container<int, my_not_default_constructible_allocator<int> >' required here
             = decltype(::new _Tp(declval<_Arg>()))>
                              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:977:24: note: in instantiation of default argument for '__test<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' required here
      static true_type __test(int);
                       ^~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:987:24: note: while substituting deduced template arguments into function template '__test' [with _Tp =
      my_container<int, my_not_default_constructible_allocator<int> >, _Arg = int, $2 = (no value)]
      typedef decltype(__test<_Tp, _Arg>(0)) type;
                       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:144:14: note: in instantiation of template class 'std::__is_direct_constructible_impl<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' requested here
    : public conditional<_B1::value, _B2, _B1>::type
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:992:14: note: in instantiation of template class 'std::__and_<std::is_destructible<my_container<int,
      my_not_default_constructible_allocator<int> > >, std::__is_direct_constructible_impl<my_container<int, my_not_default_constructible_allocator<int> >, int> >' requested here
    : public __and_<is_destructible<_Tp>,
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:1074:14: note: in instantiation of template class 'std::__is_direct_constructible_new_safe<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' requested here
    : public conditional<is_reference<_Tp>::value,
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:1082:14: note: in instantiation of template class 'std::__is_direct_constructible_new<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' requested here
    : public __is_direct_constructible_new<_Tp, _Arg>::type
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:1122:14: note: in instantiation of template class 'std::__is_direct_constructible<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' requested here
    : public __is_direct_constructible<_Tp, _Arg>
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/type_traits:1133:14: note: in instantiation of template class 'std::__is_constructible_impl<my_container<int,
      my_not_default_constructible_allocator<int> >, int>' requested here
    : public __is_constructible_impl<_Tp, _Args...>::type
             ^
<snip>

Compiler details:

$ clang --version
clang version 4.0.1-6 (tags/RELEASE_401/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Instead of SFINAEing the constructor of interest away, the implementation of std::is_constructible leads to an error.

Is the implementation of my_container's constructor incorrect?

Injunction answered 8/1, 2019 at 20:38 Comment(5)
Can't reproduce with gcc.Sefton
Here's a reproducer on Godbolt: godbolt.org/z/UI5Hy5Injunction
Looks like a clang bug. GCC and MSVS both produce the expected results.Curvaceous
CLang bug most likely.Ulrica
Thanks, I've filed a bug against clang.Injunction
P
2

Default argument initialization appears to be in the immediate context of the initialization of my_container, [meta.unary_prop]/8:

The predicate condition for a template specialization is_­constructible<T, Args...> shall be satisfied if and only if the following variable definition would be well-formed for some invented variable t:

T t(declval<Args>()...);

[ Note: These tokens are never interpreted as a function declaration. — end note  ] Access checking is performed as if in a context unrelated to T and any of the Args. Only the validity of the immediate context of the variable initialization is considered. [ Note: The evaluation of the initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. — end note  ]

According [expr.call]/7:

The initialization and destruction of each parameter occurs within the context of the calling function.

So one could deduce that the default argument initialization happens in the "immediate context". My opinion is that this is not really clear, the term immediate context does not have a formal definition.

On the other hand, Clang also considers that default function argument initialization happens in the immediate context of the initialization expression. For example this code compiles with Clang:

template<class T,class =void>
struct constructible:std::false_type{};

template<class T>
struct constructible<T,std::void_t<decltype(T{std::declval<int>()})>>:std::true_type{};

int main()
{
  static_assert(!constructible<my_container<int, my_not_default_constructible_allocator<int>>, int>::value);

  return 0;
}

So we can safely suppose this is a Clang bug.

Peptic answered 8/1, 2019 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.