public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104792] New: [g++ and/or libstdc++] Wunused-local-typedefs + C++20 concepts = annoying
@ 2022-03-04 19:07 arthur.j.odwyer at gmail dot com
  2022-03-05  0:32 ` [Bug c++/104792] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: arthur.j.odwyer at gmail dot com @ 2022-03-04 19:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104792

            Bug ID: 104792
           Summary: [g++ and/or libstdc++] Wunused-local-typedefs + C++20
                    concepts = annoying
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

This might be considered "not a bug", or "duplicate of #61596", or "bug but in
a different way from what Arthur suggests," so I'm going to give the
non-reduced test case and let one of you (@jwakely perhaps) worry about how to
reduce it.

// https://godbolt.org/z/v9Wv8vY3G
#include <iterator>
void test() {
    struct It {
        using value_type = int;
        using difference_type = int;
        int& operator*() const;
        It& operator++();       // X
        It operator++(int);     // X
    };
    static_assert(std::is_same_v<std::iter_value_t<It>, int>);
    static_assert(std::is_same_v<std::iter_rvalue_reference_t<It>, int&&>);
}

Compile with "g++ -std=c++20 -W -Wall" and libstdc++.
GCC will give a warning:

    warning: typedef 'using difference_type = int' locally defined but not used
[-Wunused-local-typedefs]
        6 |         using difference_type = int;
          |               ^~~~~~~~~~~~~~~

However, if you then delete any of the three lines marked "X", the warning will
go away again.
I believe this is because `iter_value_t<It>` relies on a C++20 Concepts
constraint where `difference_type` *is* checked when `It` is an
`input_iterator`, but is *not* checked when `It` is not an iterator. So, when
these precise three operators exist, the typedef isn't unused, but when any one
of them doesn't exist, GCC gives the unused-typedef warning.
I claim that the user-programmer shouldn't be responsible for tracking the
internal implementation details of the constraints of `std::iter_value_t`. I
just want to make a local type that has all the pieces of an iterator, without
GCC helpfully getting in the way and warning me that *right now* (according to
the internal implementation details of libstdc++) some of those pieces aren't
being used.

I admit that in this case the warning is surfaced only when I *fail* to
implement one of those three member functions, so I actually have *not*
implemented "all the pieces of an iterator" in this test case. I'm betting that
it's possible to reproduce this annoying issue, somewhere in libstdc++, even
without that caveat, though.

For now, I'll silence the warning by removing my unused typedef
`difference_type`, i.e.

#include <iterator>
void test() {
    struct It {
        using value_type = int;
        int& operator*() const;
    };
    static_assert(std::is_same_v<std::iter_value_t<It>, int>);
    static_assert(std::is_same_v<std::iter_rvalue_reference_t<It>, int&&>);
}

But I'm uncomfortable with that, because I don't know if later
revisions/bugfixes to the library will require me to re-add those pieces I've
removed.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-03-07 10:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-04 19:07 [Bug c++/104792] New: [g++ and/or libstdc++] Wunused-local-typedefs + C++20 concepts = annoying arthur.j.odwyer at gmail dot com
2022-03-05  0:32 ` [Bug c++/104792] " pinskia at gcc dot gnu.org
2022-03-06  1:07 ` arthur.j.odwyer at gmail dot com
2022-03-06  1:29 ` pinskia at gcc dot gnu.org
2022-03-06  1:39 ` [Bug c++/104792] Wunused-local-typedefs with typedef/type alias defined in struct that is defined in a function scope pinskia at gcc dot gnu.org
2022-03-07 10:46 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).