gpt4 book ai didi

c++ - 不稳定类型特征背后的原因是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:07 25 4
gpt4 key购买 nike

我今天正在调试一个失败的 clang 构建。该构建基本上失败了,因为 is_default_constructible 评估为 false。在将问题一分为二几个小时后,我将问题减少到最小的情况:

#include <type_traits>
#include <string>

namespace A {
// This has been extracted from an old (outdated(?))
// implementation of an optional type

struct empty_t {};

template<class T>
struct o
{
public:
template<class... U,
typename std::enable_if<
std::is_constructible<T, U...>::value,
bool
>::type = false
>
o(empty_t, U&&... u) { }
};

}

struct B
{
struct Foo
{
//Foo () {}; // uncomment this line and it works
bool x = true; // comment this line and it works
};

std::string c; // comment this line and it works, also change to int
A::o<Foo> r; // comment this line and it works

static const bool b;
};

static_assert(
std::is_default_constructible<B::Foo>::value,
"not constructible!");

上面的例子在 g++ 6.3 和 7.0 下编译得很好。它在 clang++ 4.0.0 和 3.9.1 中失败——静态断言仅在那个非常具体的构造中失败,但它仍然破坏了我们的构建。正如您可以自己尝试的那样,一些最小的更改可以解决问题(例如评论其中提到的行)。结果看起来有些武断。

我想知道的是,clang 中明显的错误实际上是错误还是某种未定义的行为。实际上,这部分语言的定义有多明确?

如果有任何关于如何调试此类问题的建议,我也将不胜感激:是否有一种好方法可以从 clang 中获取一些信息,说明为什么它认为 Foo 不是默认可构造的?

最后,如果你们中的任何人都可以访问(符合标准的)第三个 C++ 实现并可以报告结果,那就太好了。

最佳答案

嵌套类的默认数据成员初始值设定项在外部类完成之前(即在外部类定义的分号处)不会被实例化。当使用类型特征在嵌套类完成时(即在其定义之后)但在外部类完成之前查询时,这会导致奇怪的结果。

您可以通过将 Foo 移到 B 之外来解决这个问题。

关于c++ - 不稳定类型特征背后的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com