简体   繁体   中英

The right cast sequence from int** to int* volatile* in C++

I wanna cast T** to T * volatile * using C++ style cast. Is this right?

using namespace std;
int** p = nullptr;
auto cast_ptr = static_cast<
                    add_pointer_t<
                        add_volatile_t<
                            remove_pointer_t<decltype(p)>>>>(p);

可能更简单:

auto casted_ptr = static_cast<int * volatile *>(p);

Yes. It is right.
Here is a simple method to verify. This should work with g++.

#include <typeinfo>

cout << typeid(p).name() << endl;

And execute it like this:

$ ./a.out | c++filt -t

You will get the follow output:

int* volatile*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM