initializer_list 类
代码 initializer_list<int> a = { 10, 11, 12 }; 可以编译通过。
我想 如何自己实现 CMyClass<int> a = { 10, 11, 12 }; 也可以编译通过。
参考了 initializer_list的实现,拷贝代码 CMyClass却没法实现这样的语句: CMyClass<int> a = { 10, 11, 12 };
initializer_list的代码如下:
这个是vs2013 initializer_list类的实现代码
自己的类
相同的代码无法实现相同的功能,不知为何。
我想 如何自己实现 CMyClass<int> a = { 10, 11, 12 }; 也可以编译通过。
参考了 initializer_list的实现,拷贝代码 CMyClass却没法实现这样的语句: CMyClass<int> a = { 10, 11, 12 };
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_ */
这个是vs2013 initializer_list类的实现代码
自己的类
template<class _Elem>
class CMyClass
{ // 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;
CMyClass() _NOEXCEPT
: _First(0), _Last(0)
{ // empty list
}
CMyClass(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(CMyClass<_Elem> _Ilist) _NOEXCEPT
{ // get beginning of sequence
return (_Ilist.begin());
}
// TEMPLATE FUNCTION end
template<class _Elem> inline
const _Elem *end(CMyClass<_Elem> _Ilist) _NOEXCEPT
{ // get end of sequence
return (_Ilist.end());
}
相同的代码无法实现相同的功能,不知为何。
...全文
相关推荐
基于tensorflow加载部分层的方法
tf.variables_initializer(var_list, name='init')```这样,我们可以在恢复模型后,通过运行这个初始化器来重置特定的变量:```pythonwith tf.Session
C++类与对象的课件
5.3 构造函数和析构函数构造函数的定义通常如下:```cppclass ClassName {public: ClassName(initializer_list) { // 初始化列表 }};```
2.1深入C++构造函数共3页.pdf.zip
MyClass {public: MyClass(MyClass&& other) : value(std::move(other.value)) { } // 移动构造函数 MyClass(std::initializer_list
Python中functools模块函数解析
(cmp_func)# 排序列表sorted_list = sorted([5, 2, 9, 1], key=key_func)print(sorted_list) # 输出 [1, 2, 5, 9]`
Google custom search engine
= new CustomsearchService(initializer); // 其他搜索参数和请求设置 var request = customSearchService.Cse.List()


