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

fix emplace #1606

Merged
merged 1 commit into from
Jul 13, 2024
Merged

fix emplace #1606

merged 1 commit into from
Jul 13, 2024

Conversation

paulocoutinhox
Copy link
Contributor

No description provided.

@paulocoutinhox
Copy link
Contributor Author

Hi, can you merge? Thanks.

@pinguin999
Copy link

I don't understand the problem, but this code part blocks the build with the latest version of emscripten.

@hsdk123
Copy link

hsdk123 commented Jul 12, 2024

I don't understand the problem, but this code part blocks the build with the latest version of emscripten.

Just to give more context, the error #1611 here is shown with the latest version of emscripten.

This PR seems to fix it.

@ThePhD ThePhD merged commit d805d02 into ThePhD:develop Jul 13, 2024
p2004a added a commit to p2004a/spring that referenced this pull request Oct 5, 2024
This is just a straight copy of ThePhD/sol2#1606
from sol2 to fix compilation under clang++ 19 as it's more restrictive
in veryfying code under templates.
lhog pushed a commit to beyond-all-reason/spring that referenced this pull request Oct 6, 2024
This is just a straight copy of ThePhD/sol2#1606
from sol2 to fix compilation under clang++ 19 as it's more restrictive
in veryfying code under templates.
freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this pull request Nov 6, 2024
@cuavas
Copy link
Contributor

cuavas commented Nov 17, 2024

I don’t think this implementation of emplace makes sense for optional<T&> as written.

Consider:

	template <class T>
	class optional<T&> {
...
		template <class... Args>
		T& emplace(Args&&... args) noexcept {
			static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");

			*this = nullopt;
			new (static_cast<void*>(this)) optional(std::in_place, std::forward<Args>(args)...);
			return **this;
		}
...
	};

Now look at the static_assert – it’s testing that T can be constructed using the argument types passed to emplace. However, the optional<T&> doesn’t hold T, it logically holds T& (although it’s implemented using T* to allow assignment). The only thing that T& can be constructed with is T&. It makes no sense to use variadic arguments.

When called with T&, the static_assert only succeeds by chance when T is copy constructible. It will fail if it isn’t, and it will succeed for things it shouldn’t.

Since you know T* is trivially destructible and copyable, you could implement emplace as:

		T& emplace(T& arg) noexcept {
			m_value = &arg;
			return **this;
		}

cuavas added a commit to mamedev/mame that referenced this pull request Nov 17, 2024
sol::optional<T&>::emplace was broken, and depended on the compiler not
checking that members exist if the template wasn't instantiated.  See
ThePhD/sol2#1606 and ThePhD/sol2#1648.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants