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

Replace non-iterator, non-lambda auto with proper type #39829

Merged
merged 1 commit into from
Apr 23, 2020

Conversation

anothersimulacrum
Copy link
Member

Summary

SUMMARY: None

Purpose of change

Use proper type in place of auto

Testing

Game & tests compile and run.

Additional context

Some range based loops over std::vector<item *> were using const auto &it, where it is (obviously) of type item *.
When converting these to the proper type, they were changed to item *it, because it's both not a reference and it was being modified in the loop.
Not sure what's up with it working with auto.

@jbytheway
Copy link
Contributor

Not sure what's up with it working with auto.

You can have a reference to a pointer; that's what it was doing. Of course, this also relies on the "magic" lifetime-extension of temporaries captured by reference, which is potentially confusing.

Regardless, changes are good :).

@anothersimulacrum
Copy link
Member Author

I'd say that, but it also didn't work when it was const item &*it.

src/activity_handlers.cpp: In function ‘void activity_handlers::forage_finish(player_activity*, player*)’:
src/activity_handlers.cpp:1767:27: error: cannot declare pointer to ‘const class item&’
 1767 |         for( const item &*it : dropped ) {
      |                           ^~
src/activity_handlers.cpp:1771:50: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1771 |                 it->set_flag( flag_HIDDEN_POISON );
      |                                                  ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |               ^~~~~~~~
src/activity_handlers.cpp:1772:28: error: assignment of member ‘item::poison’ in read-only object
 1772 |                 it->poison = rng( 2, 7 );
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~
src/activity_handlers.cpp:1775:49: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1775 |                 it->set_flag( flag_HIDDEN_HALLU );
      |                                                 ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |  

Just const item *it:

src/activity_handlers.cpp: In function ‘void activity_handlers::forage_finish(player_activity*, player*)’:
src/activity_handlers.cpp:1771:50: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1771 |                 it->set_flag( flag_HIDDEN_POISON );
      |                                                  ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |               ^~~~~~~~
src/activity_handlers.cpp:1772:28: error: assignment of member ‘item::poison’ in read-only object
 1772 |                 it->poison = rng( 2, 7 );
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~
src/activity_handlers.cpp:1775:49: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1775 |                 it->set_flag( flag_HIDDEN_HALLU );
      |                                                 ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |   

@Qrox
Copy link
Contributor

Qrox commented Apr 23, 2020

It should be const item *&, i.e. a reference to a pointer. const item &* means a pointer to a reference, which doesn't exist in c++.

@anothersimulacrum
Copy link
Member Author

Ah, whoops!
Though, that doesn't work either

src/activity_handlers.cpp: In function ‘void activity_handlers::forage_finish(player_activity*, player*)’:
src/activity_handlers.cpp:1767:32: error: cannot bind non-const lvalue reference of type ‘const item*&’ to an rvalue of type ‘const item*’
 1767 |         for( const item *&it : dropped ) {
      |                                ^~~~~~~
src/activity_handlers.cpp:1771:50: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1771 |                 it->set_flag( flag_HIDDEN_POISON );
      |                                                  ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |               ^~~~~~~~
src/activity_handlers.cpp:1772:28: error: assignment of member ‘item::poison’ in read-only object
 1772 |                 it->poison = rng( 2, 7 );
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~
src/activity_handlers.cpp:1775:49: error: passing ‘const item’ as ‘this’ argument discards qualifiers [-fpermissive]
 1775 |                 it->set_flag( flag_HIDDEN_HALLU );
      |                                                 ^
In file included from src/inventory.h:19,
                 from src/character.h:32,
                 from src/avatar.h:11,
                 from src/activity_handlers.cpp:19:
src/item.h:1391:15: note:   in call to ‘item& item::set_flag(const string&)’
 1391 |         item &set_flag( const std::string &flag );
      |               ^~~~~~~~

@Qrox
Copy link
Contributor

Qrox commented Apr 23, 2020

I see, the const modifier in const auto was actually modifying the pointer, not the type, so it should probably be item *const &, i.e. a reference to a const pointer to a (non-const) item.

@ZhilkinSerg ZhilkinSerg added [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style labels Apr 23, 2020
@ZhilkinSerg ZhilkinSerg merged commit 3ad52d7 into CleverRaven:master Apr 23, 2020
@anothersimulacrum anothersimulacrum deleted the auto branch April 23, 2020 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants