gpt4 book ai didi

c++ - 当 T 声明为 T::T(T&) 时,std::is_trivially_constructible::value 的结果不正确

转载 作者:太空狗 更新时间:2023-10-29 20:43:08 27 4
gpt4 key购买 nike

这是一个让我感到困惑的小问题。我不知道如何描述它,所以只看下面的代码:

struct B {
B() {}
B(B&) {
std::cout << "not trivial\n";
}
};

int main() {
B b1;
B b2(b1);
std::cout << std::is_trivially_constructible<B, B&>::value << '\n';
return 0;
}

输出是:

not trivial
1

我正在使用 VS11。

编辑:

我刚刚测试了 http://en.cppreference.com/w/cpp/types/is_constructible 中的示例.部分输出不正确。

#include <iostream>
#include <type_traits>

class Foo {
int v1;
double v2;
public:
Foo(int n) : v1(n), v2() {}
Foo(int n, double f) : v1(n), v2(f) {}
};
int main() {
std::cout << "Foo is ...\n" << std::boolalpha
<< "\tTrivially-constructible from const Foo&? "
<< std::is_trivially_constructible<Foo, const Foo&>::value << '\n'
<< "\tTrivially-constructible from int? "
<< std::is_trivially_constructible<Foo, int>::value << '\n'
<< "\tConstructible from int? "
<< std::is_constructible<Foo, int>::value << '\n'
}

输出是:

Foo is ...
Trivially-constructible from const Foo&? true
Trivially-constructible from int? true//Trivially-constructible from int? false
Constructible from int? true

最佳答案

最终更新

在@SebastianRedl 发表了非常有见地的评论之后,我意识到该标准的意图是指对象的整个构造,而不仅仅是构造函数内部的操作。这意味着 Visual C++ 中确实存在错误。但是,我仍然认为标准的措辞不够明确,因此我将保留其余答案以供后代使用。

澄清一下:OP 提到的行为实际上是一个错误,鉴于此,我在本次更新下面所说的大部分内容都是错误的。

结束更新

这实际上不是编译器错误,而是标准的一个奇怪怪癖,所以您的困惑是可以理解的。

根据C++11标准,以下是is_trivially_constructible::value的条件成为true .

§20.9

is_constructible<T,Args...>::value is true and the variable definition for is_constructible, as defined below, is known to call no operation that is not trivial

所以,is_trivially_constructible只要给定的类型可以用给定的参数构造并且它不调用任何非平凡的操作,它就为真。您的示例仅包含一个这样的构造函数,即复制构造函数。事实上,根据“非平凡操作”(本质上是非平凡运算符或构造函数)的定义,这确实适用于您的类型。所以返回true是正确的。

但是,有一点很奇怪! C+11 标准对复制构造函数有以下说明:

§12.8.12(强调我的)

A copy/move constructor for class X is trivial if it is not user-provided and if

  • class X has no virtual functions (10.3) and no virtual base classes (10.1), and
  • the constructor selected to copy/move each direct base class subobject is trivial, and
  • for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;

    otherwise the copy/move constructor is non-trivial.

既然你确实提供了一个用户定义的复制构造函数,你的类就不是简单的复制构造函数。您提供的复制构造函数并不简单。尽管如此,非平凡的复制构造函数确实满足了 is_trivially_constructible 的必要条件。返回 true给定一个与您的复制构造函数匹配的参数。

在我看来,这似乎更像是标准中的一个“错误”。 is_trivially_constructible返回在给定特定参数的情况下该类型是否可平凡构造。这似乎并不能保证构造函数本身被认为是微不足道的!

更新:

在尝试设计一个测试来显示以下情况后,我确实在 VC11 中发现了一个错误。标准描述的逻辑意味着,如果 B用作另一种类型的子对象(成员或基类),调用 B 的复制构造函数的该类型的任何构造函数应被 std::is_trivially_constructible 视为重要 .在 VC11 中不是这样。

示例代码

#include <iostream>
#include <type_traits>

struct B
{
B() {}
B(B&) {
std::cout << "not trivial\n";
}
};

struct A : B
{
A(B& B) : b(B){}
B b;
};

int main()
{
std::cout << std::is_trivially_constructible<B, B&>::value << '\n'; // Should print 1
std::cout << std::is_trivially_constructible<A, B&>::value << '\n'; // Should print 0
getchar();
return 0;

关于c++ - 当 T 声明为 T::T(T&) 时,std::is_trivially_constructible<T, T&>::value 的结果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16232547/

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