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

Add __declspec(empty_bases) to integer_arithmetic and floating_point_arithmetic #124

Closed
mathysie opened this issue Nov 30, 2021 · 2 comments

Comments

@mathysie
Copy link
Contributor

mathysie commented Nov 30, 2021

In our implementation of type_safe we use __declspec(empty_bases) as an optimalisation. The result is that the size of the resulting struct is equal to the size of an int or double or anything we want to make type_safe. For example:

template<typename T>
struct __declspec(empty_bases) CIntStrongType : type_safe::strong_typedef<T, int>,
												type_safe::strong_typedef_op::equality_comparison<T>,
												type_safe::strong_typedef_op::relational_comparison<T>,
												type_safe::strong_typedef_op::mixed_relational_comparison<T, int>,
												type_safe::strong_typedef_op::unary_plus<T>,
												type_safe::strong_typedef_op::unary_minus<T>,
												type_safe::strong_typedef_op::addition<T>,
												type_safe::strong_typedef_op::subtraction<T>,
												type_safe::strong_typedef_op::multiplication<T>,
												type_safe::strong_typedef_op::division<T>,
												type_safe::strong_typedef_op::modulo<T>,
												type_safe::strong_typedef_op::increment<T>,
												type_safe::strong_typedef_op::decrement<T>
{
	using type_safe::strong_typedef<T, int>::strong_typedef;
};
static_assert(sizeof(CIntStrongType<struct TestType>) == sizeof(int));

However, the structs floating_point_arithmetic and integer_arithmetic do not have __declspec(empty_bases). Therefore, if we would replace some structs with integer_arithmetic the implementation would be

template<typename T>
struct __declspec(empty_bases) CIntStrongType2 : type_safe::strong_typedef<T, int>,
												type_safe::strong_typedef_op::equality_comparison<T>,
												type_safe::strong_typedef_op::relational_comparison<T>,
												type_safe::strong_typedef_op::mixed_relational_comparison<T, int>,
												type_safe::strong_typedef_op::integer_arithmetic<T>
{
	using type_safe::strong_typedef<T, int>::strong_typedef;
};
static_assert(sizeof(CIntStrongType2<struct TestType>) > sizeof(int));

which uses more memory than the first version, while it has the same implementation.

Is it possible to add __declspec(empty_bases) to structs to enhance optimization?

@foonathan
Copy link
Owner

That seems to be a good idea. Please do a PR, as I don't have access to MSVC to test it out.

@mathysie
Copy link
Contributor Author

Thanks! I'll create a PR in due time. I think I'll do it in my Christmas break

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

No branches or pull requests

2 participants