向函数指针添加constness时会发生什么
What happens when adding constness to a function pointer?
静态断言都失败。什么类型是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, "");
}
函数指针不变:
static_assert(std::is_same<typename Constifier<int (*)()>::Type, int (*)()>::value, "");
你不能改变一个函数,因为它存在于内存的代码区,所以你可以认为函数指针隐式地指向const。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 向函数指针添加constness时会发生什么