我不明白為什么 Clang-TidyClang-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
uj5u.com熱心網友回復:
這看起來像一個虛假的診斷。我會全域禁用它,因為這個用例很常見。
uj5u.com熱心網友回復:
從錯誤中我了解到,作為型別(或類似型別)
"hello world"
的陣列在某個地方 被衰減為型別const char[11]
const char*
std::forward
"hello world"
是 achar const[12]
并衰減到char const*
當您構造一個臨時std::string
呼叫Foo
的建構式時。
但是為什么以及如何解決此錯誤?
抑制該警告的一種方法是:
Bar bar(std::string{ "hello world" });
std::make_shared<Foo>("hello world")
std::forward
在用法和確實有效方面非常相似。
這很有趣:
Foo foo0("hello world"); // No warning
auto foo1 = std::make_shared<Foo>("hello world"); // No warning
Bar bar0("hello world"); // Warning
auto bar1 = std::make_shared<Bar>("hello world"); // Warning
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469971.html