我不明白为什么 Clang-Tidy 会产生错误 Clang-Tidy: Do not implicitly decay an array into a pointer
在以下示例中:
struct Foo {
explicit Foo(std::string _identifier) : identifier(std::move(_identifier)) {}
std::string identifier;
};
struct Bar {
template<typename... Ts>
explicit Bar(Ts &&...args) : foo(std::forward<Ts>(args)...) {} // <-- Error
Foo foo;
};
Bar bar("hello world");
从错误中我了解到 "hello world"
是 const char[11]
类型的数组(或类似的)被衰减为类型 const char*
在 std::forward
期间的某处.但是为什么以及如何修复此错误? std::make_shared<Foo>("hello world")
关于 std::forward
的用法非常相似并且确实有效。
最佳答案
这看起来像一个虚假的诊断。我会在全局范围内禁用它,因为这个用例很常见。
关于c++ - 'Clang-Tidy:在使用 std::forward 和 const char* 时,不要将数组隐式衰减为指针',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72101801/