2

The following C++20 code is accepted by both clang and GCC but rejected by the Sonar static analyzer with "requires clause differs in template redeclaration":

template <typename T>
concept Underlying = /* expression omitted */;

using Frac = std::int16_t;

namespace detail {
template <typename T, typename W, Frac F>
concept ValidParams = /* expression omitted */;
}

template <Underlying T, Underlying W, Frac F = 0>
requires detail::ValidParams<T, W, F>
class FixedPoint final
{
    template <Underlying R, Underlying V, Frac E>
    requires detail::ValidParams<R, V, E> //< requires clause differs in template redeclaration
    friend class FixedPoint;
//...

I skimmed over the relevant chapter of the C++ standard (13. Templates) but found no special requirements on the requires-clause in class template declarations, and the case of a friend class template with requires-clause does not appear to be explicitly covered (nor does it have to be as a friend declaration is not substantially different from an ordinary declaration).

My question is whether this is a defect of the static analyzer or my code is non-compliant.

cigien
  • 57,834
  • 11
  • 73
  • 112
Pavel Kirienko
  • 1,162
  • 1
  • 15
  • 31
  • I'm not sure why you're trying to make `FixedPoint` a friend of itself. What is that intended to do? – cigien Apr 26 '23 at 11:05
  • 1
    This is a standard idiom for sharing access to private fields between different instantiations of the class template. – Pavel Kirienko Apr 26 '23 at 11:19
  • As a rule of thumb, if the compiler accepts something but the static analyzer claims it's ill-formed, the compiler should be assumed to be correct. – Brian Bi Apr 29 '23 at 15:00

0 Answers0