Skip to content

False positive warning warning: instantiation of variable 'declvar<A>' required here, but no definition is available, when the definition is not required #128657

Open
@HolyBlackCat

Description

Consider the following code: https://gcc.godbolt.org/z/acd5W9YMn

struct A {explicit constexpr operator bool() const {return true;}};
struct B {int x = 1; explicit constexpr operator bool() const {return x;}};

template <typename T>
extern T declvar;

template <auto T>
constexpr int dummy = 0;

template <typename T>
concept C = requires{dummy<declvar<T> ? true : false>;};

static_assert(C<A>);
static_assert(!C<B>);

This compiles correctly on all 3 bug compilers including Clang (and seems to be the only good way of doing this).

But Clang emits a useless warning:

<source>:11:28: warning: instantiation of variable 'declvar<A>' required here, but no definition is available [-Wundefined-var-template]
   11 | concept C = requires{dummy<declvar<T> ? true : false>;};
      |                            ^
<source>:5:10: note: forward declaration of template entity is here
    5 | extern T declvar;
      |          ^
<source>:11:28: note: add an explicit instantiation declaration to suppress this warning if 'declvar<A>' is explicitly instantiated in another translation unit
   11 | concept C = requires{dummy<declvar<T> ? true : false>;};
      |                            ^

The variable definition isn't needed here, so it shouldn't warn.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions