Почему это использование std :: is_constructible не компилируется? - PullRequest
0 голосов
/ 08 января 2019

Один из конструкторов моего контейнера default-конструирует распределитель как значение параметра по умолчанию:

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

Предположительно, этот конструктор включен только тогда, когда Allocator может быть создан по умолчанию.

Я хотел бы проверить, с std::is_constructible, может ли этот конструктор использоваться с распределителем, который не является конструктивным по умолчанию:

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

Однако, когда я применяю 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;
}

Выход компилятора:

$ 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>

Сведения о компиляторе:

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

Вместо SFINAE, исключая интересующий конструктор, реализация std::is_constructible приводит к ошибке.

Не верна ли реализация конструктора my_container?

1 Ответ

0 голосов
/ 08 января 2019

Инициализация аргумента по умолчанию, по-видимому, происходит в непосредственном контексте инициализации my_container, [meta.unary_prop] / 8 :

Условие предиката для специализации шаблона is_­constructible<T, Args...> должно быть выполнено тогда и только тогда, когда следующее определение переменной будет правильно сформировано для некоторой изобретенной переменной t:

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

[Примечание: эти токены никогда не интерпретируются как объявление функции. - конец примечания ] Проверка доступа выполняется как в контексте, не связанном с T и любым из Args. Рассматривается только допустимость непосредственного контекста инициализации переменной. [Примечание: оценка инициализации может привести к побочным эффектам, таким как создание специализаций шаблонов классов и специализаций шаблонов функций, генерация неявно определенных функций и т. Д. Такие побочные эффекты не находятся в «непосредственном контексте» и могут привести к неправильной работе программы. - конец примечания ]

Согласно [expr.call] / 7:

Инициализация и уничтожение каждого параметра происходит в контексте вызывающей функции.

Таким образом, можно сделать вывод, что инициализация аргумента по умолчанию происходит в «непосредственном контексте». Мое мнение таково, что это не совсем понятно, термин непосредственный контекст не имеет формального определения.

С другой стороны, Clang также считает, что инициализация аргумента функции по умолчанию происходит в непосредственном контексте выражения инициализации. Например, этот код компилируется с помощью 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;
}

Так что мы можем смело предположить, что это ошибка Clang.

...