简体   繁体   中英

What happens when adding constness to a function pointer?

Static assertions all fail. What type is Constifier creating for a function pointer?

#include <type_traits>

template<typename T>
struct Constifier;

template<typename T>
struct Constifier<T *>
{
    typedef const T *Type;
};

int main()
{
    static_assert(std::is_same<typename Constifier<int (*)()>::Type, const int (*)()>::value, "");
    static_assert(std::is_same<typename Constifier<int (*)()>::Type, int (*const)()>::value, "");
    static_assert(std::is_same<typename Constifier<int (*)()>::Type, void>::value, "");
}

The function pointer is unchanged:

static_assert(std::is_same<typename Constifier<int (*)()>::Type, int (*)()>::value, "");

You cannot alter a function because it lives in the code section of the memory, so you can think of a function pointer implicity pointing to const already.

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