提问人:Raymund Hofmann 提问时间:6/24/2022 更新时间:6/24/2022 访问量:72
std::d estroy_at 主要编译器之间的差异?
std::destroy_at differences between major compilers?
问:
将编译器资源管理器与以下功能一起使用:
#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 不符合或配置错误?
答:
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只能为类类型添加专用化,而不能为函数添加专用化。
评论
shared_ptr
std::destroy_at
std::make_shared
pv->~U()
std::destroy_at
std::destroy_at