将constness添加到函数指针时会发生什么?

时间:2015-09-06 19:08:51

标签: c++ templates typetraits

静态断言都失败了。为函数指针创建的Constifier是什么类型的?

#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, "");
}

1 个答案:

答案 0 :(得分:0)

函数指针不变:

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

你不能改变一个函数,因为它存在于内存的代码部分,所以你可以想到函数指针的含义已经指向const。

相关问题