ERROR C3848:具有类型"const XXX" 的表达式会丢失一些 const-volatile 限定符以调用"YYY" with"ZZZ"
今天看书,Thinking in c++ volume 2 "Adaptable function objects"
里面作者说:
Suppose, for example, that we want to make the function object gt_n, defined
earlier in this chapter, adaptable. All we need to do is the following:class gt_n : public unary_function<int, bool> {
int value;
public:
gt_n(int val) : value(val) {}
bool operator()(int n) {
return n > value;
}
};
然后我做了一个template
#ifndef GREATERTHANN_HPP
#define GREATERTHANN_HPP #include <functional> namespace ZJ {
template <class ArgType, class Result = bool>
struct gt_n : public std::unary_function<ArgType, Result>
{
private:
ArgType value;
public:
gt_n(const ArgType& val) : value(val) {}
Result operator()(ArgType n) { return (n > value); }
ArgType getVal() { return value; }
};
} #endif // GREATERTHANN_HPP
测试代码:
//: C06:GreaterThanN.cpp #include "GreaterThanN.hpp" #include <iostream>
#include <functional>
#include <algorithm> using namespace ZJ;
using namespace std; int main() {
int a[] = { , , , , -, -, };
const size_t SIZE = sizeof a / sizeof a[]; gt_n<int> predicate();
cout << "gt_n.getVal() = " << predicate.getVal() << endl; cout << count_if(a, a + SIZE, not1(predicate)) << endl; //
return ;
} ///:~
编译时报错
: error C3848: 具有类型“const ZJ::gt_n<int,bool>”的表达式会丢失一些 const-vola
tile 限定符以调用“bool ZJ::gt_n<int,bool>::operator ()(ArgType)”
with
[
ArgType=int
]
看起来大概意思是:你用的gt_n<int, bool>的instance具有const属性,但是调用该instance的表达式(也就是“bool ZJ::gt_n<int,bool>::operator ()(ArgType))不具有const属性,丢失const,所以无法通过编译
最开始我在看main里面,predicate不具有const属性啊
后来去查std::not1的文档:http://en.cppreference.com/w/cpp/utility/functional/not1
template< class Predicate >
std::unary_negate<Predicate> not1(const Predicate& pred);
因为std::not1用的是const Predicate&,所以我的predicate到not1之中以后就是const Predicate&了,所以在调用operator()的时候要求operator()也具有const属性,否则就会导致丢失const限定符的错误
因此解决办法就是给operator()加上const属性,如下:
#ifndef GREATERTHANN_HPP
#define GREATERTHANN_HPP #include <functional> namespace ZJ {
template <class ArgType, class Result = bool>
struct gt_n : public std::unary_function<ArgType, Result>
{
private:
ArgType value;
public:
gt_n(const ArgType& val) : value(val) {}
Result operator()(ArgType n) const { return (n > value); }
ArgType getVal() { return value; }
};
} #endif // GREATERTHANN_HPP
好了,现在可以通过编译了。
你可以从上面std::not1文档的Example代码看出,operator()是加了const属性的,因此这个地方是书的作者写错了。
ERROR C3848:具有类型"const XXX" 的表达式会丢失一些 const-volatile 限定符以调用"YYY" with"ZZZ"的更多相关文章
- ISO/IEC 9899:2011 条款6.7.3——类型限定符
6.7.3 类型限定符 语法 1.type-qualifier: const restrict volatile _Atomic 约束 2.除了指针类型(其被引用的类型是一个对象类型)之外的类型,不应 ...
- C语言中类型限定符
通常用类型和存储类别来描述一个变量. C90还增加了两个属性:恒常性(constancy).易变性(volatility): 分别用关键字const和volatile来声明. 这两个关键字创建的类型是 ...
- C 类型限定符
C 类型限定符 1. Introduction C 语言中的大部分类型都可以用称为限定符(qualifier)的关键字 const. volatile. restrict. _Atomic 加以限定. ...
- 类型限定符volatile
目录 类型限定符volatile 强制内存读取 禁止编译优化 注意:volatile不能够保证线程同步 volatile bool flag; volatile int a; 添加volatile限定 ...
- C#调用Excel报 error CS1969: 找不到编译动态表达式所需的一个或多个类型。是否缺少引用?
转自[http://blog.csdn.net/bodybo/article/details/43191319] 程序需要读取Exel文件,有如下代码段 object oMissing = Syste ...
- c语言的类型、运算符与表达式
title: 2017-10-17c语言的类型.运算符与表达式 tags: c程序设计语言 grammar_cjkRuby: true --- 1.1 数据类型 char 字符型,一个字节 int 整 ...
- 变量和基本类型——复合类型,const限定符,处理类型
一.复合类型 复合类型是指基于其他类型定义的类型.C++语言有几种复合类型,包括引用和指针. 1.引用 引用并非对象,它只是为一个已存在的对象所起的另外一个名字. 除了以下2种情况,其他所有引用的类型 ...
- const限定符、constexpr和常量表达式------c++ primer
编译器将在编译过程中把用到const变量的地方都替换成对应的值,为了执行这种替换,编译器必须知道变量的初始值.如果程序包含多个文件,则那个用了const对象的文件都必须能访问到它的初始值才行.要做到这 ...
- 2变量与基本类型之const限定符
一.const介绍: const对象一旦被创建其值就不能再改变,所以const对象必须初始化.任何试图对const赋值的行为都会引发错误. 二.初始化和const: 对const对象的主要限制就是只能 ...
随机推荐
- Orchard运用 - 网站样例
在此分享一些个人觉得比较不错的基于Orhcard搭建的网站. 中文版: http://www.58img.com/ http://www.weijiqiong.com/ http://www.apma ...
- kvm安装windows系统
1.创建虚拟机镜像文件并指定大小(10G) [root@centos01 ~]# qemu-img create -f raw /opt/windows20031.raw 10G Formatting ...
- 通过form表单上传文件获取后台传来的数据
小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...
- 数据库插入数据返回当前自增主键ID值的方法
当我们插入一条数据的时候,我们很多时候都想立刻获取当前插入的主键值返回以做它用.我们通常的做法有如下几种: 1. 先 select max(id) +1 ,然后将+1后的值作为主键插入数据库: 2. ...
- org.hibernate.MappingException: An association from the table order_intem_inf refers to a unmapped
执行一个HIbernate的演示样例时出现例如以下错误信息 Exception in thread "main" java.lang.ExceptionInInitializerE ...
- QtGui.QProgressBar
A progress bar is a widget that is used when we process lengthy tasks. It is animated so that the us ...
- Js 的test方法
Js 的test()方法 test() 方法用于检测一个字符串是否匹配某个模式. 定义和用法test() 方法用于检测一个字符串是否匹配某个模式. 如果字符串中有匹配的值返回 true ,否则返回 f ...
- Bitmap尺度变换
Bitmap bitMap = BitmapFactory.decodeFile(path); int width = bitMap.getWidth(); int height = bitMap.g ...
- Android开发牛刀小试之“AA算钱软件”开发(一)
事实上想去做android开发已经有非常长一段时间了,可是因为还在上课,加上老板那边的项目接连不断.也一直都没有机会抽出身来做.可是,楼主当然也不会闲着,首先我了解到android开发须要java学习 ...
- 如何下载flash离线安装包
如何下载flash离线安装包 CreateTime--2018年4月14日16:02:13 Author:Marydon 1.下载地址 UpdateTime--2018年5月13日16点55分 p ...