4

I do not understand why Clang-Tidy produces the error Clang-Tidy: Do not implicitly decay an array into a pointer in the following example:

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");

From the error I understand that "hello world" being an array of type const char[11] (or similar) is decayed to type const char* somewhere during std::forward. But why and how can I fix this error? std::make_shared<Foo>("hello world") is very similar regarding usage of std::forward and does work.

2
  • Have you tried Bar bar((char const *)"hello world"); ? Does it still show error? Although it is not a nice fix, still it is interesting if it fixes the problem. Commented May 3, 2022 at 16:09
  • This does not trigger the error as the cast is eventually an explicit decay Commented May 4, 2022 at 5:22

2 Answers 2

3

This looks like a bogus diagnostic. I would disable it globally, since this usecase is so common.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, seems to be the case.
0

From the error I understand that "hello world" being an array of type const char[11] (or similar) is decayed to type const char* somewhere during std::forward

"hello world" is a char const[12] and decays to char const* when you construct a temporary std::string to call Foo's constructor.

But why and how can I fix this error?

A way to suppress that warning is:

Bar bar(std::string{ "hello world" });

std::make_shared<Foo>("hello world") is very similar regarding usage of std::forward and does work.

This is interesting:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.