包含了一个initializer_list头文件,为什么向它的形参传递一个值的序列,把这个序列放在花括号中编译器显示说不对 附:编译器是VS2012

tiffinysmile 2015-10-30 04:31:03
initializer_list 头文件
#pragma once
#ifndef _INITIALIZER_LIST_
#define _INITIALIZER_LIST_
#ifndef RC_INVOKED
#include <cstddef>

#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new

_STD_BEGIN
// TEMPLATE CLASS initializer_list
template<class _Elem>
class initializer_list
{ // list of pointers to elements
public:
typedef _Elem value_type;
typedef const _Elem& reference;
typedef const _Elem& const_reference;
typedef size_t size_type;

typedef const _Elem* iterator;
typedef const _Elem* const_iterator;

initializer_list() _NOEXCEPT
: _First(0), _Last(0)
{ // empty list
}

initializer_list(const _Elem *_First_arg,
const _Elem *_Last_arg) _NOEXCEPT
: _First(_First_arg), _Last(_Last_arg)
{ // construct with pointers
}

const _Elem *begin() const _NOEXCEPT
{ // get beginning of list
return (_First);
}

const _Elem *end() const _NOEXCEPT
{ // get end of list
return (_Last);
}

size_t size() const _NOEXCEPT
{ // get length of list
return ((size_t)(_Last - _First));
}

private:
const _Elem *_First;
const _Elem *_Last;
};

// TEMPLATE FUNCTION begin
template<class _Elem> inline
const _Elem *begin(initializer_list<_Elem> _Ilist) _NOEXCEPT
{ // get beginning of sequence
return (_Ilist.begin());
}

// TEMPLATE FUNCTION end
template<class _Elem> inline
const _Elem *end(initializer_list<_Elem> _Ilist) _NOEXCEPT
{ // get end of sequence
return (_Ilist.end());
}
_STD_END

#pragma pop_macro("new")
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _INITIALIZER_LIST_ */

编写一个主函数

#include<iostream>
#include"initializer_list.h"
using namespace std;
void add(initializer_list<int> a)
{
int sum =0;
for(auto beg = a.begin(),end = a.end();beg != end;beg++)
{

sum+=*beg;
}
cout << sum <<endl;

}
void main()
{

add ({2,1,0});



}
...全文
184 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2015-11-03
  • 打赏
  • 举报
回复
LZ用的2012
  • 打赏
  • 举报
回复
引用 4 楼 ForestDB 的回复:
VC对C++标准的支持还得等等。
这个。。。。VS 2013已经支持了,已经是2年前的事情了。
  • 打赏
  • 举报
回复
引用 6 楼 ForestDB 的回复:
LZ用的2012
2012不是明确不支持嘛,升级编译器就能解决的事情,为啥要等等编译器支持呢
ForestDB 2015-11-02
  • 打赏
  • 举报
回复
VC对C++标准的支持还得等等。
  • 打赏
  • 举报
回复
引用 2 楼 tiffinysmile 的回复:
麻烦回答一下依据这个头文件,怎么修改才能让向initializer_list传递参数
2012 不支持,至少得vs2013才行
tiffinysmile 2015-10-30
  • 打赏
  • 举报
回复
麻烦回答一下依据这个头文件,怎么修改才能让向initializer_list传递参数
dustpg 2015-10-30
  • 打赏
  • 举报
回复

想用新特性就用最新的

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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