C++ 为什么在函数类型中添加'const'不能使'is_const'为真?
考虑这些:C++ 为什么在函数类型中添加'const'不能使'is_const'为真?,c++,language-lawyer,C++,Language Lawyer,考虑这些: std::is_const<void() const>::value == false std::is_same<void() const, std::remove_const_t<void() const>>::value == true std::is_const::value==false std::is_same::value==true 我本以为函数类型void()const会被视为const 为什么不呢?自由函数不能是const,因
std::is_const<void() const>::value == false
std::is_same<void() const, std::remove_const_t<void() const>>::value == true
std::is_const::value==false
std::is_same::value==true
我本以为函数类型void()const
会被视为const
为什么不呢?自由函数不能是
const
,因此将忽略应用于自由函数类型的const
只有成员函数可以是const
[C++14:8.3.5/6]:
cv限定符seq或ref限定符只能是以下内容的一部分:
- 非静态成员函数的函数类型,
- 指向成员的指针所引用的函数类型
- 函数类型定义声明或别名声明的顶级函数类型
- 类型参数(14.1)的默认参数中的类型id,或
- 类型参数(14.2)的模板参数的类型id
那么,我该如何摆脱这种束缚呢?我正在获取一个常量限定的自由函数作为模板参数,无法生成此类型的函数指针:
指向函数类型“long(int,double)const”的指针不能具有“const”限定符F*fp^代码>@user697683:这样做,您就不会得到一个const限定的自由函数作为模板参数。这样的事情是不存在的。问题出在源头!