Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False postive: Forward declaration suggested for template declaration with definition in specialization #1217

Closed
glennvl opened this issue Mar 21, 2023 · 3 comments
Milestone

Comments

@glennvl
Copy link

glennvl commented Mar 21, 2023

Summary

I have a template class without implementation, and a specialization where the actual implementation lives. As is also used for std::function. Include-what-you-use incorrectly suggests to add a forward declaration for the template class without implementation.

Source code

// static_function.hpp

namespace util {

template <typename TFunction, std::size_t StorageSize = sizeof(std::uintptr_t) * 3u>
class StaticFunction;

template <std::size_t StorageSize, typename TRet, typename... TArgs>
class StaticFunction<TRet(TArgs...), StorageSize>
{
    // implementation
};

}
// static_function.test.cpp

#include <static_function.hpp>

// unittests that use StaticFunction live here

IWYU output

[build] 3/4 Building CXX object tests/CMakeFiles/test_utils.dir/static_function.test.cpp.o
[build] Warning: include-what-you-use reported diagnostics:
[build]
[build] /home/glenn/projects/STM32F303-DISCOVERY/tests/static_function.test.cpp should add these lines:
[build] namespace util { template <typename TFunction, std::size_t StorageSize = sizeof(std::uintptr_t) * 3U> class StaticFunction; }

Note that the forward declaration suggested by IWYU not only does not make sense, it is also not valid C++ (because it contains the default template arguments).

Extra information

Tested with two IWYU versions

  • include-what-you-use 0.17 based on Ubuntu clang version 13.0.1-2ubuntu2.1
  • built from master against clang 16
@kimgr
Copy link
Contributor

kimgr commented Mar 25, 2023

Could you share also what the uses in static_function_test.cpp look like?

@glennvl
Copy link
Author

glennvl commented Mar 25, 2023

Could you share also what the uses in static_function_test.cpp look like?

See https://gist.github.com/glennvl/c03845c3c6499af6e9aa8308e56f2247

@kimgr kimgr added this to the iwyu 0.22 milestone Dec 17, 2023
@kimgr
Copy link
Contributor

kimgr commented Dec 17, 2023

I can't reproduce on master (but none of the repro cases are really free-standing enough that I could test them without modification, so I can't say for sure if I'm trying the same thing). There's been a bunch of template improvements, so I'm going to close this as fixed. Please reopen with a reduced example if the problems persist.

@kimgr kimgr closed this as completed Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants