模板函数的参数类型不能通过表达式推导
来源:互联网 发布:linux系统查看系统时间 编辑:程序博客网 时间:2024/05/21 14:47
I want to implement a container which can accept initial list of elements by supplying the constructor with sequence of values.
9 template <typename C, int max> 10 struct c_array 11 { 12 typedef C value_type; 13 14 typedef C* iterator; 15 typedef const C* const_iterator; 16 17 typedef C& reference; 18 typedef const C& const_reference; 19 20 c_array() { } 21 template <class T> 22 c_array(typename T::iterator begin,typename T::iterator end) 23 { 24 } 25 26 C v[max]; 27 operator C*() { return v; } 28 29 reference operator[] (ptrdiff_t i) { return v[i]; } 30 const_reference operator[] (ptrdiff_t i) const { return v[i]; } 31 32 iterator begin() { return v; } 33 const_iterator begin() const { return v; } 34 35 iterator end() { return v+max; } 36 const_iterator end() const { return v+max; } 37 38 size_t size() const { return max; } 39 }; 40 41 int main(int argc, char** argv) 42 { 43 std::vector<int> myvector(10,10); 44 c_array<int,10> myarray1(myvector.begin(),myvector.end()); ...
I am getting the following error while compiling
... test.cc:56:61: error: no matching function for call to ‘c_array<int, 10>::c_array(std::vector<int>::iterator, std::vector<int>::iterator)’ test.cc:56:61: note: candidates are: test.cc:22:9: note: template<class T> c_array::c_array(typename T::iterator, typename T::iterator) test.cc:22:9: note: template argument deduction/substitution failed: test.cc:56:61: note: couldn't deduce template parameter ‘T’ ...
Thanks in advance,
1 Answer
In the declaration
template <class T>c_array(typename T::iterator begin,typename T::iterator end)
T
is used in a way called a "non-deduced context". There's no reasonable way, if you pass in arguments of type MyCustomIter
, for the compiler to guess what type T
might contain a typedef MyCustomIter iterator;
. So the C++ Standard says compilers shouldn't even try.
Instead you can just write the function to take any type and assume/document that it should be an iterator.
template <class InputIter>c_array(InputIter begin, InputIter end) { std::copy_n(begin, max, v);}
I want to implement a container which can accept initial list of elements by supplying the constructor with sequence of values.
9 template <typename C, int max> 10 struct c_array 11 { 12 typedef C value_type; 13 14 typedef C* iterator; 15 typedef const C* const_iterator; 16 17 typedef C& reference; 18 typedef const C& const_reference; 19 20 c_array() { } 21 template <class T> 22 c_array(typename T::iterator begin,typename T::iterator end) 23 { 24 } 25 26 C v[max]; 27 operator C*() { return v; } 28 29 reference operator[] (ptrdiff_t i) { return v[i]; } 30 const_reference operator[] (ptrdiff_t i) const { return v[i]; } 31 32 iterator begin() { return v; } 33 const_iterator begin() const { return v; } 34 35 iterator end() { return v+max; } 36 const_iterator end() const { return v+max; } 37 38 size_t size() const { return max; } 39 }; 40 41 int main(int argc, char** argv) 42 { 43 std::vector<int> myvector(10,10); 44 c_array<int,10> myarray1(myvector.begin(),myvector.end()); ...
I am getting the following error while compiling
... test.cc:56:61: error: no matching function for call to ‘c_array<int, 10>::c_array(std::vector<int>::iterator, std::vector<int>::iterator)’ test.cc:56:61: note: candidates are: test.cc:22:9: note: template<class T> c_array::c_array(typename T::iterator, typename T::iterator) test.cc:22:9: note: template argument deduction/substitution failed: test.cc:56:61: note: couldn't deduce template parameter ‘T’ ...
Thanks in advance,
1 Answer
In the declaration
template <class T>c_array(typename T::iterator begin,typename T::iterator end)
T
is used in a way called a "non-deduced context". There's no reasonable way, if you pass in arguments of type MyCustomIter
, for the compiler to guess what type T
might contain a typedef MyCustomIter iterator;
. So the C++ Standard says compilers shouldn't even try.
Instead you can just write the function to take any type and assume/document that it should be an iterator.
template <class InputIter>c_array(InputIter begin, InputIter end) { std::copy_n(begin, max, v);}
- 模板函数的参数类型不能通过表达式推导
- 字符串参数的模板函数推导问题
- C++函数模板的参数推导
- 模板参数的推导
- 函数模板的模板参数自动推导探究
- 函数模板参数推导的两个经典应用
- C++——函数模板的参数推导
- C++——函数模板的参数推导
- 函数模板参数推导的两个经典应用
- C2970: “vt::vector”: 模板参数, 涉及带有内部链接的对象的表达式不能用作非类型参数 备忘
- 模板函数中指针作为调用参数时模板参数的推导问题
- 图说函数模板右值引用参数(T&&)类型推导规则(C++11)
- C++11 图说VS2013下的引用叠加规则和模板参数类型推导规则
- item 1: 对于模板类型的推导
- [c++]模板的类型推导--数组
- 模板类型推导
- 模板参数推导
- 模板参数自动推导
- hibernate
- Java 8中获取参数名称
- 新手守则——关于Android屏幕适配
- 静态构造函数和静态类
- ubuntu下修改文件夹权限
- 模板函数的参数类型不能通过表达式推导
- go语言学习 1-初识go语言
- linux sort命令详解
- IO之内核buffer----"buffer cache"
- 01 - VirtualBox+Ubuntu12.04安装
- 【模板下载】分享我所使用的数据库框架
- Hibernate SQL查询 addScalar()或addEntity()
- hibernate
- App如何适应 iPhone 5s/6/6 Plus 三种屏幕的尺寸?(iOS的屏幕适配问题)