我遇到了一个相当麻烦的情况,我试图通过我编写的一个在其构造函数中接受std::any的类来使用std::make_any,但在尝试提出一个最小的测试用例时,遇到了我认为可能是std::any和/或std::is_copy_constructible实现中的错误,但是我不是百分之百确定的。这里有几个使用g++8和clang++9的不同工作或不工作的最小示例。在每个示例中,所有相同的代码行都存在,但在两行被注释或未注释的4种状态下,以及结果。
我使用的是C++17标准,将-std=c++17标志传递给g++-8和clang++-9,它们都使用libstdc++版本8.3.0 (Ubuntu18.04)。
下面的代码使用g++-8和clang++-9进行编译。
#include <any>
#include <type_traits>
struct Hippo {
Hippo () { }
Hippo (Hippo const &) { }
Hippo (Hippo &&) { }
// Hippo (std::any) { }
};
int main (int argc, char **argv) {
static_assert(std::is_copy_constructible_v<Hippo>);
// auto w = std::make_any<Hippo>();
return 0;
}正如人们所预期的那样,下面的方法也适用于这两种情况。
#include <any>
#include <type_traits>
struct Hippo {
Hippo () { }
Hippo (Hippo const &) { }
Hippo (Hippo &&) { }
// Hippo (std::any) { }
};
int main (int argc, char **argv) {
static_assert(std::is_copy_constructible_v<Hippo>);
auto w = std::make_any<Hippo>();
return 0;
}下面的代码使用g++-8编译,但使用clang++-9编译失败。这一点令人困惑,因为std::is_copy_constructible_v<Hippo>的值不应该因为添加一个与复制构造无关的构造函数而改变。
#include <any>
#include <type_traits>
struct Hippo {
Hippo () { }
Hippo (Hippo const &) { }
Hippo (Hippo &&) { }
Hippo (std::any) { }
};
int main (int argc, char **argv) {
static_assert(std::is_copy_constructible_v<Hippo>);
// auto w = std::make_any<Hippo>();
return 0;
}clang++-9给出的误差是(从长度上裁剪):
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:132:31: error: no member named 'value' in 'std::is_copy_constructible<Hippo>'
: public conditional<_B1::value, _B2, _B1>::type
~~~~~^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/any:170:17: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<Hippo>, std::is_constructible<Hippo, const Hippo &> >' requested here
enable_if<__and_<is_copy_constructible<_Tp>,在这里,看起来std::is_copy_constructible<Hippo>类型在使用点(在<any>头中)是不完整的,但是它应该有一个布尔值的constexpr value成员。我认为这是一个错误,因为g++8可以很好地编译它。大概这个bug是在libstdc++的std::any和/或std::is_copy_constructible的实现中出现的,但是因为g++-8可以工作,而clang++-9失败了,所以这是令人困惑的。
但是,以下代码无法在g++-8和clang++-9中编译:
#include <any>
#include <type_traits>
struct Hippo {
Hippo () { }
Hippo (Hippo const &) { }
Hippo (Hippo &&) { }
Hippo (std::any) { }
};
int main (int argc, char **argv) {
static_assert(std::is_copy_constructible_v<Hippo>);
auto w = std::make_any<Hippo>();
return 0;
}不出所料,clang++-9产生的错误是相同的,但g++-8的错误声称,所有模板参数推导的候选者都不能使用std::any的构造函数,包括应该可以工作的构造函数,但该构造函数中隐藏的是std::is_copy_constructible的用法,所以我认为弹出的是相同的问题,并且我非常确定这是一个错误。我已经去掉了错误消息中不相关的部分:
/usr/include/c++/8/any: In instantiation of ‘std::any std::make_any(_Args&& ...) [with _Tp = Hippo; _Args = {}]’:
.../any.cpp:13:35: required from here
/usr/include/c++/8/any:431:14: error: no matching function for call to ‘std::any::any(const std::in_place_type_t<Hippo>&)’
return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<...cut for length...>
/usr/include/c++/8/any:208:7: note: candidate: ‘template<class _ValueType, class ... _Args, class _Tp, class _Mgr, typename std::enable_if<std::__and_<std::is_copy_constructible<_Tp>, std::is_constructible<_Tp, _Args&& ...> >::value, bool>::type <anonymous> > std::any::any(std::in_place_type_t<_Tp>, _Args&& ...)’
any(in_place_type_t<_ValueType>, _Args&&... __args)
^~~
/usr/include/c++/8/any:208:7: note: template argument deduction/substitution failed:
<...cut for length...>不管怎样,我的问题是--这是libstdc++中的一个bug,对吧?
发布于 2020-03-26 14:57:07
为了充分研究这个问题,我尝试使用libc++ (仅使用clang++-9 )而不是libstdc++进行编译,这解决了问题。因此,这似乎是libstdc++ 8.3.0中的一个合法错误。
https://stackoverflow.com/questions/60862278
复制相似问题