std::d estroy_at 主要编译器之间的差异?

std::destroy_at differences between major compilers?

提问人:Raymund Hofmann 提问时间:6/24/2022 更新时间:6/24/2022 访问量:72

问:

将编译器资源管理器与以下功能一起使用:

#include <iostream>
#include <memory>

struct test
{
  test(int i)
  {
    std::cout << "test::test("<<i<<")\n";
  }
  ~test()
  {
    std::cout << "~test()\n";
  }
};

template<>
void std::destroy_at(test* p) 
{
  std::cout<<"std::destroy_at<test>\n";
  p->~test();
}

int
main ()
{
  auto sp = std::make_shared<test>(3);
  return 33;
}

使用 C++ 20 和 gcc x86-64 或 clang x86-64 给出预期输出:

Program returned: 33
test::test(3)
std::destroy_at<test>
~test()

但是 x64 msvc v19.32 提供了:

Program returned: 33
test::test(3)
~test()

好像 std::d estroy_at 在这里没有效果。

这是符合行为、我的误解还是 msvc 不符合或配置错误?

C GCC 可视化-C++ 嚓咔嚓 STD

评论

0赞 NathanOliver 6/24/2022
AFAIK不需要在其开发器中使用。shared_ptrstd::destroy_at
0赞 Yksisarvinen 6/24/2022
然而,这很有趣,因为标准明确要求当对象是通过 创建时,它后来会用 而不是 来销毁。eel.is/c++draft/util.smartptr.shared.create#7.11更令人困惑的是,gcc 和 clang 都只在 C++20 模式 (UB) 下调用:godbolt.org/z/zMPK31EET,但在 C++17 模式(无 UB)时不调用:godbolt.org/z/saxE5o19fstd::make_sharedpv->~U()std::destroy_atstd::destroy_at

答:

5赞 HolyBlackCat 6/24/2022 #1

从C++20开始,标准库函数是UB。

评论

0赞 Raymund Hofmann 6/24/2022
“只有当声明依赖于至少一个程序定义的类型并且专用化满足原始模板的所有要求时,才允许将任何标准库函数模板的模板专用化添加到命名空间 std,除非禁止此类专用化。”是否确定?
3赞 Yksisarvinen 6/24/2022
@RaymundHofmann 直到 20 C++
0赞 Guillaume Racicot 6/24/2022
@RaymundHofmann只能为类类型添加专用化,而不能为函数添加专用化。