Skip to content

Conversation

rolandreichweinbmw
Copy link
Contributor

I have a recurring use case of globally allocated class instances that I need to explicitly construct in a more defined order than implicit construction of global C++ objects can offer.

Therefore, I have a convenience wrapper etl::typed_storage for aligned_storage_as for instantiating an object explicitly at a defined time.

Previously, I tried with etl::optional, which basically supports the same interface I need, but I can't use this one because it always references ~T. But I want the compiler to optimize away the unneeded dtors because the global objects won't get destroyed anyway until power-off in many embedded scenarios.

(You can still explicitly destroy() if needed.)

@jwellbelove jwellbelove changed the base branch from master to pull-request/#1023-Alignment-typed-storage March 3, 2025 11:10
@jwellbelove
Copy link
Contributor

If you can implement these changes, then I can include this in the next release.

@rolandreichweinbmw
Copy link
Contributor Author

It is implemented in the PR, in etl::typed_storage, isn't it? (As demonstrated in the unit tests.)

@jwellbelove
Copy link
Contributor

It says my requested review changes are 'Pending'.

@rolandreichweinbmw
Copy link
Contributor Author

I have the feeling I'm missing sth. here. :-) Can you please point me to your change requests? I currently can't see any in

#1023

or

https://github.com/ETLCPP/etl/pull/1023/files

?

public:

typed_storage_error(string_type file_name_, numeric_type line_number_)
: alignment_exception(ETL_ERROR_TEXT("typed_storage:error", ETL_ALIGNMENT_FILE_ID"A"), file_name_, line_number_)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be ETL_ALIGNMENT_FILE_ID"B" as it's the second alignment_exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted accordingly.

{
ETL_ASSERT(!has_value(), ETL_ERROR(etl::typed_storage_error));
valid = true;
return *new (data.template get_address<char>()) value_type(etl::forward<Args>(args)...);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use ::new here to explicitly reference the global new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted accordingly.

/// \returns the instance of T which has been constructed in the internal byte array.
//***************************************************************************
template<typename... Args>
reference emplace(Args&&... args)
Copy link
Contributor

Choose a reason for hiding this comment

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

For the other classes in the ETL that have a member function that constructs an object, I have used create(Args&&... args).
I can see the reason for using emplace, but it would be inconsistent naming.
Also, create is the common antonym of destroy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? emplace() is already being used in more ETL classes than create() is:

$ grep -r 'create(Args' include/etl/ -m1 |wc -l
5
$ grep -r 'emplace(Args' include/etl/ -m1 |wc -l
13
$

@rolandreichweinbmw
Copy link
Contributor Author

In the last commit in the PR, I replaced emplace() with create(). So depending how you decide regarding this name question above, you can take this one or leave this last commit out.

@rolandreichweinbmw
Copy link
Contributor Author

The feature is already available in the feature extended version of ETL at https://github.com/rolandreichweinbmw/etlplus

@jwellbelove jwellbelove merged commit 7d91e1f into ETLCPP:pull-request/#1023-Alignment-typed-storage Apr 2, 2025
63 checks passed
@github-project-automation github-project-automation bot moved this from In progress to Done in Embedded Template Library Apr 2, 2025
jwellbelove pushed a commit that referenced this pull request Apr 12, 2025
rolandreichweinbmw added a commit to rolandreichweinbmw/etl that referenced this pull request Sep 8, 2025
In a recent change to alignment.h, the etl::typed_storage was
changed in a way that in case of an already constructed object,
the object is created via assignment.

However, this contradicts the original use case that led to
etl::typed_storage in the first place:
ETLCPP#1023

The goal is to omit destructors (and at the same time support
classes with deleted assignment operators), so they can be optimized out
at link time.

This change reverts commit ac7b268 to restore the original
functionality and changes the test to reflect the original
use case.
rolandreichweinbmw added a commit to rolandreichweinbmw/etl that referenced this pull request Sep 8, 2025
In a recent change to alignment.h, the etl::typed_storage was
changed in a way that in case of an already constructed object,
the object is created via assignment.

However, this contradicts the original use case that led to
etl::typed_storage in the first place:
ETLCPP#1023

The goal is to omit destructors (and at the same time support
classes with deleted assignment operators), so they can be optimized out
at link time.

This change reverts commit ac7b268 to restore the original
functionality and changes the test to reflect the original
use case.
rolandreichweinbmw added a commit to rolandreichweinbmw/etl that referenced this pull request Sep 8, 2025
In a recent change to alignment.h, the etl::typed_storage was
changed in a way that in case of an already constructed object,
the object is created via assignment.

However, this contradicts the original use case that led to
etl::typed_storage in the first place:
ETLCPP#1023

The goal is to omit destructors (and at the same time support
classes with deleted assignment operators), so they can be optimized out
at link time.

This change reverts commit ac7b268 to restore the original
functionality and changes the test to reflect the original
use case.
jwellbelove added a commit that referenced this pull request Sep 10, 2025
* Added coderabbitai configuration

* Added builtin mem function tests

* Modified etl::typed_storage

* Modified etl::typed_storage

# Conflicts:
#	include/etl/alignment.h

* Added ETL_NOEXCEPT and ETL_NOEXCEPT_IF_NO_THROW

* Added etl::typed_storage_ext and swap for same

* Added etl::typed_storage_ext and swap for same

# Conflicts:
#	include/etl/alignment.h

* Added release notes

* Fixes to GCC -O2 errors

* Changed char* parameters to value_type* parameters

* Fixed compilation issues for const containers unit tests

* Added automatic selection of __builtin_memxxx functions for GCC and clang

* Added enhanced coderabbit configuration

* Updated version and release notes

* Disabled constexpr const container tests for C++11

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Updated version and release notes

* feat: removed unreachable break statements (#1169)

* Updated version and release notes

* Modified etl::typed_storage

# Conflicts:
#	include/etl/alignment.h

* Fix etl::typed_storage by supporting omitted destructors

In a recent change to alignment.h, the etl::typed_storage was
changed in a way that in case of an already constructed object,
the object is created via assignment.

However, this contradicts the original use case that led to
etl::typed_storage in the first place:
#1023

The goal is to omit destructors (and at the same time support
classes with deleted assignment operators), so they can be optimized out
at link time.

This change reverts commit ac7b268 to restore the original
functionality and changes the test to reflect the original
use case.

* Fix missing create() in non-C++11 typed_storage_ext constructor

* Typo fix

---------

Co-authored-by: John Wellbelove <[email protected]>
Co-authored-by: Drew Rife <[email protected]>
Co-authored-by: John Wellbelove <[email protected]>
jwellbelove added a commit that referenced this pull request Sep 11, 2025
* Added coderabbitai configuration

* Added builtin mem function tests

* Modified etl::typed_storage

* Modified etl::typed_storage

# Conflicts:
#	include/etl/alignment.h

* Added ETL_NOEXCEPT and ETL_NOEXCEPT_IF_NO_THROW

* Added etl::typed_storage_ext and swap for same

* Added etl::typed_storage_ext and swap for same

# Conflicts:
#	include/etl/alignment.h

* Added release notes

* Fixes to GCC -O2 errors

* Changed char* parameters to value_type* parameters

* Fixed compilation issues for const containers unit tests

* Added automatic selection of __builtin_memxxx functions for GCC and clang

* Added enhanced coderabbit configuration

* Updated version and release notes

* Disabled constexpr const container tests for C++11

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Attempted fixes for MacOS compilation

* Updated version and release notes

* feat: removed unreachable break statements (#1169)

* Updated version and release notes

* Modified etl::typed_storage

# Conflicts:
#	include/etl/alignment.h

* Fix etl::typed_storage by supporting omitted destructors

In a recent change to alignment.h, the etl::typed_storage was
changed in a way that in case of an already constructed object,
the object is created via assignment.

However, this contradicts the original use case that led to
etl::typed_storage in the first place:
#1023

The goal is to omit destructors (and at the same time support
classes with deleted assignment operators), so they can be optimized out
at link time.

This change reverts commit ac7b268 to restore the original
functionality and changes the test to reflect the original
use case.

* Fix missing create() in non-C++11 typed_storage_ext constructor

* Typo fix

---------

Co-authored-by: John Wellbelove <[email protected]>
Co-authored-by: Drew Rife <[email protected]>
Co-authored-by: John Wellbelove <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants