**********折磨编译器的一句代码,程序在 vs2003 上编译通过,但是在 GCC3.3.4 和 GCC 4.1.2 和VS2005 中都编译失败

hzhxxx 2010-10-28 09:52:23

#include <map>
#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mod_equal(std::pair<const std::string,std::string> &p1,
std::pair<const std::string,std::string> &p2 )
{
return p1.first == p2.first;
}

//去处 multimap 里面重复数据的方法
int main(int argc,char *argv[])
{
std::multimap<std::string,std::string> val;
val.insert(make_pair("aaaa","abc"));
val.insert(make_pair("aaaa","cccc"));
val.insert(make_pair("vvv","fsdf"));
val.insert(make_pair("vvv","ggggg"));
val.insert(make_pair("aaaa","cdddsa"));
val.insert(make_pair("bbb","cdddsa"));

std::multimap<std::string,std::string>::iterator it;
for(it = val.begin(); it != val.end();++it)
{
std::cout<<it->first<<" " <<it->second<<endl;
}

std::cout<<endl;
std::vector<std::pair<std::string,std::string> > val_a;
//这个是简写
std::unique_copy(val.begin(),val.end(),std::back_inserter(val_a),mod_equal);
//这个是非简写
//std::unique_copy(val.begin(),val.end(),std::back_inserter<std::vector<std::pair<std::string,std::string> > >(val_a),mod_equal);
//程序在 vs2003 上编译通过,但是在 GCC3.3.4 和 GCC 4.1.2 和VS2005 中都编译失败
for(size_t i = 0; i < val_a.size();++i)
{
std::cout<<val_a[i].first<<" "<<val_a[i].second<<endl;
}

//system("pause");
}

...全文
322 28 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzhxxx 2010-10-29
  • 打赏
  • 举报
回复
楼主回帖精神不错,我这里是这么定义的:

// TEMPLATE CLASS multimap
template<class _Kty,
class _Ty,
class _Pr = less<_Kty>,
class _Alloc = allocator<pair<const _Kty, _Ty> > >
class multimap
: public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> >
{ // ordered red-black tree of {key, mapped} values, non-unique keys
public:
typedef multimap<_Kty, _Ty, _Pr, _Alloc> _Myt;
typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> > _Mybase;
typedef typename _Mybase::const_reverse_iterator
const_reverse_iterator;
typedef typename _Mybase::value_type value_type;
};

// TEMPLATE CLASS _Tmap_traits
template<class _Kty, // key type
class _Ty, // mapped type
class _Pr, // comparator predicate type
class _Alloc, // actual allocator type (should be value allocator)
bool _Mfl> // true if multiple equivalent keys are permitted
class _Tmap_traits
{ // traits required to make _Tree behave like a map
public:
typedef _Kty key_type;
typedef pair<const _Kty, _Ty> value_type;
typedef _Pr key_compare;
typedef typename _Alloc::template rebind<value_type>::other
太乙 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hzhxxx 的回复:]
template<class _FwdIt,
class _OutIt,
class _Pr> inline
_OutIt _Unique_copy(_FwdIt _First, _FwdIt _Last, _OutIt _Dest, _Pr _Pred,
forward_iterator_tag)
{ // copy compressing pairs satisfying _Pred……
[/Quote]lz这么肯定?我从你给的代码中没看出来啊????!!!你看看operator=的操作
hastings 2010-10-28
  • 打赏
  • 举报
回复
应该就是8楼的原因,
另,谓词函数既然用了应用,那么按惯例应该用const&
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

taodm

(taodm(不能处理站内信))

等 级:


看来环境也不错,都有
VC2008express、GCC4.4
这种利器了。
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

那看来还是可以的,个版本差异比较大,我的

gcc --version
gcc (GCC) 4.1.2 20070115 (prerelease) (SUSE Linux)

这个编译失败。

看来这个是个 bug,

GCC4.4均编译运行正常

又修复了
superarhow 2010-10-28
  • 打赏
  • 举报
回复
偶的文件头部是这样的:
#include "stdafx.h"
#include <string>
#include <map>
#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
main最后加了个return 0;打印出的结果是:
aaaa abc
aaaa cccc
aaaa cdddsa
bbb cdddsa
vvv fsdf
vvv ggggg

aaaa abc
bbb cdddsa
vvv fsdf

VS版本是:
Microsoft Visual C++ 2005
Version 8.0.50727.762 SP.050727-7600
taodm 2010-10-28
  • 打赏
  • 举报
回复
加了#include <string> 后VC2008express、GCC4.4均编译运行正常。
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

to
superarhow
(苏泊尔耗)

VS2005可以的呀
LZ MS都没有#include <string>嘛


你的真的可以吗,是不是确认编译通过,运行成功了。
我的就算 #include <string> 也是不可以的


superarhow 2010-10-28
  • 打赏
  • 举报
回复
VS2005可以的呀
LZ MS都没有#include <string>嘛

hzhxxx 2010-10-28
  • 打赏
  • 举报
回复


这个不敢苟同,看了一下代码,这里 vs2005 的实现有待商榷,为什么要对一个 __value 赋值两次,没有必要啊。
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

template<class _FwdIt,
class _OutIt,
class _Pr> inline
_OutIt _Unique_copy(_FwdIt _First, _FwdIt _Last, _OutIt _Dest, _Pr _Pred,
forward_iterator_tag)
{ // copy compressing pairs satisfying _Pred, forward iterators
_FwdIt _Firstb = _First;

for (*_Dest++ = *_Firstb; ++_First != _Last; )
if (!_Pred(*_Firstb, *_First))
_Firstb = _First, *_Dest++ = *_Firstb;
return (_Dest);
}

这个是 vs2003 的实现,

template<class _Kty,
class _Ty,
class _Pr = less<_Kty>,
class _Alloc = allocator<pair<const _Kty, _Ty> > >
class multimap
: public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true> >
{ // ordered red-black tree of {key, mapped} values, non-unique keys
public:

pair<const,> 的实现肯定是 const key 的
taodm 2010-10-28
  • 打赏
  • 举报
回复
VC2003是甚至比VC6还bug多的垃圾版本,直接扔了就行了,不值得讨论。
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

我也看到了这个,对 pair<const key,> 的 __value 第一次构造成*__first,第二次赋值失败

typename iterator_traits<_InputIterator>::value_type __value = *__first;
1220 *__result = __value;
1221 while (++__first != __last)
1222 if (!__binary_pred(__value, *__first))
1223 {
1224 __value = *__first;
太乙 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hzhxxx 的回复:]
这点我明白,所以 mod_equal 函数是这么声明的:
bool mod_equal(std::pair<const std::string,std::string> &p1,
std::pair<const std::string,std::string> &p2 );
[/Quote]对pair的赋值,key是不能改的!vs2003可能实现的是使用pair<string, string>而不是pair<const string, string>,lz可以看看它的源码~!
太乙 2010-10-28
  • 打赏
  • 举报
回复
lz试试这个:
#include <iostream>
#include <string>
using namespace std;

int main()
{
pair<const string, string> p("hello", "world");
pair<const string, string> p2;
p2 = p;
return 0;
}
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

这点我明白,所以 mod_equal 函数是这么声明的:
bool mod_equal(std::pair<const std::string,std::string> &p1,
std::pair<const std::string,std::string> &p2 );
太乙 2010-10-28
  • 打赏
  • 举报
回复
lz需要明白,multimap/map的value_type是pair<const key, value>
hzhxxx 2010-10-28
  • 打赏
  • 举报
回复

补充说明一下,VS2003 编译通过,并能运行的到正确的结果.
GCC 4.1.2 报错如下:
/usr/include/c++/4.1.2/bits/stl_pair.h:69: error: non-static const member 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::first', can't use default assignment operator
太乙 2010-10-28
  • 打赏
  • 举报
回复
1209 __unique_copy(_InputIterator __first, _InputIterator __last,
1210 _OutputIterator __result,
1211 _BinaryPredicate __binary_pred,
1212 output_iterator_tag)
1213 {
1214 // concept requirements -- iterators already checked
1215 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1216 typename iterator_traits<_InputIterator>::value_type,
1217 typename iterator_traits<_InputIterator>::value_type>)
1218
1219 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1220 *__result = __value;
1221 while (++__first != __last)
1222 if (!__binary_pred(__value, *__first))
1223 {
1224 __value = *__first;
1225 *++__result = __value;
1226 }
1227 return ++__result;
1228 }


请看红色标注的。。。。
luciferisnotsatan 2010-10-28
  • 打赏
  • 举报
回复
确实。
加载更多回复(7)
嵌入式高级网络设备开发, 目 录 第一章 快速上手指南...................................................................................................... 1 第二章 产 品 简 介........................................................................................................ 6 第三章 软 件 系 统........................................................................................................ 8 3.1 编译环境,烧写内核等相关说明.......................................................................... 9 3.1.1 建立宿主机开发环境............................................................................ 9 3.1.2 文件与目录结构.................................................................................. 16 3.1.3 内核编译.............................................................................................. 17 3.1.4 利用Redboot 烧制内核映像和文件系统........................................... 24 3.1.5 编译引导代码RedBoot........................................................................ 33 3.1.6 利用RAM 版redboot 更新ROM 版redboot..................................... 34 3.1.7 安装编译JTAG 烧写工具.................................................................... 36 3.1.8 利用JTAG 烧写redboot(引导代码)............................................. 37 3.1.9 图形配置工具configtool 的编译和使用............................................ 39 3.2 软件应用开发....................................................................................................... 40 3.2.1 开发模式.............................................................................................. 40 3.2.2 如何创建编译自己的应用.................................................................. 41 3.2.3 应用程序调试方法.............................................................................. 42 3.2.4 制作文件系统映像.............................................................................. 44 3.3 设备驱动开发(以UCLINUX 2.4 内核介绍)............................................................ 47 3.3.1 简介...................................................................................................... 47 3.3.2 设备驱动程序的框架.......................................................................... 49 3.3.3 设备驱动编程注意事项...................................................................... 50 3.3.4 添加自己的设备驱动.......................................................................... 51 第四章 外围接口使用介绍............................................................................................ 53 第五章 硬 件 系 统...................................................................................................... 54 5.1 功能模块结构图................................................................................................... 54 5.2 各个部分的构成................................................................................................... 54 5.3 片选、总线........................................................................................................... 55 5.5 接口管脚说明....................................................................................................... 57 第六章 机 械 特 性...................................................................................................... 58 机械特性......................................................................................................................... 58

65,199

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧