Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

c-style casting to c++ #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

c-style casting to c++ #173

wants to merge 3 commits into from

Conversation

smefpw
Copy link

@smefpw smefpw commented Oct 19, 2019

changes made to item_definitions
change some variables to const
[[nodiscard]] tag added to icon_override

> changes made to item_definitions
> change some variables to const
> [[nodiscard]] tag added to icon_override
Copy link
Owner

@namazso namazso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, the quick and dirty casts for offsets were intentionally left C style, because C++ cast doesn't really improve (nonexistent) readability there, but I wouldn't mind changing them

@@ -195,7 +195,7 @@ class config
return m_icon_overrides;
}

auto get_icon_override(const std::string_view original) const -> const char*
[[nodiscard]] auto get_icon_override(const std::string_view original) const -> const char*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodiscard is supposed to be used when discarding the result results in something bad, like memory leak
here it is just meaningless to do so, but not actively harmful

@@ -90,7 +90,7 @@ class table_hook
auto hook_function(Fn hooked_fn, const std::size_t index) -> Fn
{
m_new_vmt[index] = (void*)(hooked_fn);
return (Fn)(m_old_vmt[index]);
return static_cast<Fn>(m_old_vmt[index]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C casting is intentional in templates, since "Fn" could be a type that cannot be static casted to from void*, like a function type. Maybe MSVC allows this, but it shouldn't, by standard function and object pointers (like void*) are not castable to each other with static_cast

Copy link
Author

@smefpw smefpw Oct 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiles and runs fine but from what you're saying reinterpret_cast may work? (Maybe dynamic_cast? But I'm not really sure what that one does)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"compiles and runs on my machine" is not the same as standard compilant code.
according to standard conversion between pointer to object and pointer to function must be a reinterpret cast, and even then it is platform defined behavior. Clang also considers it an error:
https://godbolt.org/z/x6T_8M
Why would I (or anyone for that matter) write platform dependent and nonstandard code without any benefits?
Anyways, I've rewritten this in master to not use conversion across object ptr and function ptr, since it is platform defined behavior according to the standard

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aight I'll keep this in mind

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants