奇怪的std :: is_constructible

问题描述

让我们看看这个:

#include <vector>
#include <type_traits>

// somewhere in main
using V = std::vector<int&>;

好的,引用向量没有任何意义,但是模板实例化仅在“外部”级别上起作用-如果不使用内部实体,则不会实例化它们。因此,此代码将编译。

但是

bool value = std::is_constructible_v<V>;

不编译!为什么?我除了value应该只是一个false

更具体地说,我不明白为什么在decltype的典型实现中此代码(我的意思是is_constructible内的表达式)为什么会引起编译器错误而不是SFINAE:

namespace std {
namespace detail {
template <class,class T,class... Args>
struct is_constructible_helper : false_type {};

template <class T,class... Args>
struct is_constructible_helper<void_t<decltype(T{declval<Args>()...})>,T,Args...> : true_type {};
} // namespace detail

template <class T,class... Args>
using is_constructible = detail::is_constructible_helper<void,Args...>;
} // namespace std

来自gcc的错误消息如下所示:

In file included from [varIoUs standard includes]
In instantiation of 'class __gnu_cxx::new_allocator<int&>':
required from [chain of compiler-created classes]
error: forming pointer to reference type 'int&'
    allocate(size_type __n,const void* = static_cast<const void*>(0))
    ^~~~~~~~
error: forming pointer to reference type 'int&'
    deallocate(_TP* __p,size_type __t)
    ^~~~~~~~~~

和一些类似的消息,它们指向标准库的头文件的其他部分。这些错误后会出现一条错误消息,指出push_back(const value_type& __x)无法重载push_back(value_type&& __x)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)