kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Storage open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Storage

Avoid dropInstance uncaught errors

3.5.0-beta2

v0.11.1

v0.25.2

localForage

Avoid dropInstance uncaught errors

seaweedfs

Cloudreve

3.5.0-beta2

go-ipfs

v0.11.1

thanos

v0.25.2

Popular Libraries in Storage

Trending New libraries in Storage

Top Authors in Storage

1

57 Libraries

29583

2

45 Libraries

5580

3

27 Libraries

413

4

18 Libraries

4797

5

16 Libraries

7535

6

16 Libraries

68

7

15 Libraries

1067

8

14 Libraries

472

9

14 Libraries

3707

10

13 Libraries

4684

1

57 Libraries

29583

2

45 Libraries

5580

3

27 Libraries

413

4

18 Libraries

4797

5

16 Libraries

7535

6

16 Libraries

68

7

15 Libraries

1067

8

14 Libraries

472

9

14 Libraries

3707

10

13 Libraries

4684

Trending Kits in Storage

No Trending Kits are available at this moment for Storage

Trending Discussions on Storage

    Why is std::aligned_storage to be deprecated in C++23 and what to use instead?
    `Firebase` package was successfully found. However, this package itself specifies a `main` module field that could not be resolved
    After upgrading from Angular 12 to 13, cache is too large for Github
    android:exported added but still getting error Apps targeting Android 12 and higher are required to specify an explicit value for android:exported
    How to fix: "@angular/fire"' has no exported member 'AngularFireModule'.ts(2305) ionic, firebase, angular
    How to solve FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore problem?
    How is optional assignment constexpr in C++ 20?
    No analytics cookies is set upon a consent was updated
    Do not nest files in rails active storage
    What should happen if one calls `std::exit` in a global object's destructor?

QUESTION

Why is std::aligned_storage to be deprecated in C++23 and what to use instead?

Asked 2022-Apr-11 at 14:18

I just saw that C++23 plans to deprecate both std::aligned_storage and std::aligned_storage_t as well as std::aligned_union and std::aligned_union_t.

Placement new'd objects in aligned storage are not particularly constexpr friendly as far as I understand, but that doesn't appear to be a good reason to throw out the type completely. This leads me to assume that there is some other fundamental problem with using std::aligned_storage and friends that I am not aware of. What would that be?

And is there a proposed alternative to these types?

ANSWER

Answered 2022-Apr-11 at 14:18

Here are three excerpts from P1413R3:

Background

aligned_* are harmful to codebases and should not be used. At a high level:

  • Using aligned_* invokes undefined behavior (The types cannot provide storage.)
  • The guarantees are incorrect (The standard only requires that the type be at least as large as requested but does not put an upper bound on the size.)
  • The API is wrong for a plethora of reasons (See "On the API".)
  • Because the API is wrong, almost all usage involves the same repeated pre-work (See "Existing usage".)

On the API

std::aligned_* suffer from many poor API design decisions. Some of these are shared, and some are specific to each. As for what is shared, there are three main problems [only one is included here for brevity]:

  • Using reinterpret_cast is required to access the value

There is no .data() or even .data on std::aligned_* instances. Instead, the API requires you to take the address of the object, call reinterpret_cast<T*>(...) with it, and then finally indirect the resulting pointer giving you a T&. Not only does this mean that it cannot be used in constexpr, but at runtime it's much easier to accidentally invoke undefined behavior. reinterpret_cast being a requirement for use of an API is unacceptable.


Suggested replacement

The easiest replacement for aligned_* is actually not a library feature. Instead, users should use a properly-aligned array of std::byte, potentially with a call to std::max(std::initializer_list<T>) . These can be found in the <cstddef> and <algorithm> headers, respectively (with examples at the end of this section). Unfortunately, this replacement is not ideal. To access the value of aligned_*, users must call reinterpret_cast on the address to read the bytes as T instances. Using a byte array as a replacement does not avoid this problem. That said, it's important to recognize that continuing to use reinterpret_cast where it already exists is not nearly as bad as newly introducing it where it was previously not present. ...

The above section from the accepted proposal to retire aligned_* is then followed with a number of examples, like these two replacement suggestions:

copy icondownload icon

1// To replace std::aligned_storage
2template &lt;typename T&gt;
3class MyContainer {
4private:
5    //std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; t_buff;
6    alignas(T) std::byte t_buff[sizeof(T)];
7};
8

copy icondownload icon

1// To replace std::aligned_storage
2template &lt;typename T&gt;
3class MyContainer {
4private:
5    //std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; t_buff;
6    alignas(T) std::byte t_buff[sizeof(T)];
7};
8// To replace std::aligned_union
9template &lt;typename... Ts&gt;
10class MyContainer {
11private:
12    //std::aligned_union_t&lt;0, Ts...&gt; t_buff;
13    alignas(Ts...) std::byte t_buff[std::max({sizeof(Ts)...})];
14};
15

Source https://stackoverflow.com/questions/71828288

Community Discussions contain sources that include Stack Exchange Network

    Why is std::aligned_storage to be deprecated in C++23 and what to use instead?
    `Firebase` package was successfully found. However, this package itself specifies a `main` module field that could not be resolved
    After upgrading from Angular 12 to 13, cache is too large for Github
    android:exported added but still getting error Apps targeting Android 12 and higher are required to specify an explicit value for android:exported
    How to fix: &quot;@angular/fire&quot;' has no exported member 'AngularFireModule'.ts(2305) ionic, firebase, angular
    How to solve FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore problem?
    How is optional assignment constexpr in C++ 20?
    No analytics cookies is set upon a consent was updated
    Do not nest files in rails active storage
    What should happen if one calls `std::exit` in a global object's destructor?

QUESTION

Why is std::aligned_storage to be deprecated in C++23 and what to use instead?

Asked 2022-Apr-11 at 14:18

I just saw that C++23 plans to deprecate both std::aligned_storage and std::aligned_storage_t as well as std::aligned_union and std::aligned_union_t.

Placement new'd objects in aligned storage are not particularly constexpr friendly as far as I understand, but that doesn't appear to be a good reason to throw out the type completely. This leads me to assume that there is some other fundamental problem with using std::aligned_storage and friends that I am not aware of. What would that be?

And is there a proposed alternative to these types?

ANSWER

Answered 2022-Apr-11 at 14:18

Here are three excerpts from P1413R3:

Background

aligned_* are harmful to codebases and should not be used. At a high level:

  • Using aligned_* invokes undefined behavior (The types cannot provide storage.)
  • The guarantees are incorrect (The standard only requires that the type be at least as large as requested but does not put an upper bound on the size.)
  • The API is wrong for a plethora of reasons (See "On the API".)
  • Because the API is wrong, almost all usage involves the same repeated pre-work (See "Existing usage".)

On the API

std::aligned_* suffer from many poor API design decisions. Some of these are shared, and some are specific to each. As for what is shared, there are three main problems [only one is included here for brevity]:

  • Using reinterpret_cast is required to access the value

There is no .data() or even .data on std::aligned_* instances. Instead, the API requires you to take the address of the object, call reinterpret_cast<T*>(...) with it, and then finally indirect the resulting pointer giving you a T&. Not only does this mean that it cannot be used in constexpr, but at runtime it's much easier to accidentally invoke undefined behavior. reinterpret_cast being a requirement for use of an API is unacceptable.


Suggested replacement

The easiest replacement for aligned_* is actually not a library feature. Instead, users should use a properly-aligned array of std::byte, potentially with a call to std::max(std::initializer_list<T>) . These can be found in the <cstddef> and <algorithm> headers, respectively (with examples at the end of this section). Unfortunately, this replacement is not ideal. To access the value of aligned_*, users must call reinterpret_cast on the address to read the bytes as T instances. Using a byte array as a replacement does not avoid this problem. That said, it's important to recognize that continuing to use reinterpret_cast where it already exists is not nearly as bad as newly introducing it where it was previously not present. ...

The above section from the accepted proposal to retire aligned_* is then followed with a number of examples, like these two replacement suggestions:

copy icondownload icon

1// To replace std::aligned_storage
2template &lt;typename T&gt;
3class MyContainer {
4private:
5    //std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; t_buff;
6    alignas(T) std::byte t_buff[sizeof(T)];
7};
8

copy icondownload icon

1// To replace std::aligned_storage
2template &lt;typename T&gt;
3class MyContainer {
4private:
5    //std::aligned_storage_t&lt;sizeof(T), alignof(T)&gt; t_buff;
6    alignas(T) std::byte t_buff[sizeof(T)];
7};
8// To replace std::aligned_union
9template &lt;typename... Ts&gt;
10class MyContainer {
11private:
12    //std::aligned_union_t&lt;0, Ts...&gt; t_buff;
13    alignas(Ts...) std::byte t_buff[std::max({sizeof(Ts)...})];
14};
15

Source https://stackoverflow.com/questions/71828288