为什么不使用std :: is_constructible编译?

Jar*_*ock 9 c++ sfinae type-traits

我的容器的一个构造函数默认构造一个分配器作为默认参数值:

template<class T, class Allocator>
struct my_container
{
  my_container(int n, Allocator alloc = Allocator()) {}
};
Run Code Online (Sandbox Code Playgroud)

据推测,只有在Allocator默认构造时才启用此构造函数.

我想测试一下std::is_constructible,这个构造函数是否可以与一个不是默认构造的分配器一起使用:

template<class T>
struct my_not_default_constructible_allocator
{
  // no default ctor
  my_not_default_constructible_allocator(int) {}
};
Run Code Online (Sandbox Code Playgroud)

但是,当我申请时std::is_constructible,我得到一个编译时错误,而不是false,这是我所期望的:

#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;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

$ 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>
Run Code Online (Sandbox Code Playgroud)

编译器细节:

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

而不是SFINAEing感兴趣的构造函数,实现std::is_constructible导致错误.

是不是my_container构造函数的实现不正确?

Oli*_*liv 2

默认参数初始化似乎是在[meta.unary_prop]/8my_container初始化的直接上下文中:

\n\n
\n

is_\xc2\xadconstructible<T, Args...>当且仅当以下变量定义对于某些发明变量 t 来说是格式良好的时,才应满足模板特化的谓词条件:

\n
\n\n
T t(declval<Args>()...);\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

[\xe2\x80\x89注意:这些标记永远不会被解释为函数声明。\n \xe2\x80\x94\xe2\x80\x89end note\n \xe2\x80\x89]\n 访问检查的执行就像在与 T 和任何 Args 无关的上下文中。\n 仅考虑变量初始化的直接上下文的有效性。\n [\xe2\x80\x89注意:初始化的评估可能会导致副作用,例如类模板特化和函数模板特化的实例化、隐式定义函数的生成等等。\n 此类副作用不在 \xe2\x80\x9ci 直接上下文\xe2\x80\x9d 中,并且可能导致程序格式错误。\n \xe2\x80\x94\xe2\x80\x89end note\n \xe2\x80\x89]

\n
\n\n

根据 [expr.call]/7:

\n\n
\n

每个参数的初始化和销毁​​都发生在调用函数的上下文中。

\n
\n\n

因此可以推断默认参数初始化发生在“直接上下文”中。我的观点是,这并不是很清楚,术语“直接上下文”没有正式的定义。

\n\n

另一方面,Clang 还认为默认函数参数初始化发生在初始化表达式的直接上下文中。例如,此代码使用 Clang 编译:

\n\n
template<class T,class =void>\nstruct constructible:std::false_type{};\n\ntemplate<class T>\nstruct constructible<T,std::void_t<decltype(T{std::declval<int>()})>>:std::true_type{};\n\nint main()\n{\n  static_assert(!constructible<my_container<int, my_not_default_constructible_allocator<int>>, int>::value);\n\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以我们可以放心地假设这是一个 Clang bug。

\n