模板函数的参数类型不能通过表达式推导



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,

share improve this question
 

1 Answer

up vote 5 down vote accepted

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);
}
share improve this answer
 
 
+1 for clearly explaining why you need that change. An additional note that may or may not be new to the OP: for ordinary functions, it isn't necessarily be a problem if template deduction cannot succeed, as long it's possible to explicitly specify the template arguments. No such syntax exists for templated constructors, so a templated constructor where the template arguments cannot be deduced will never be callable. –   hvd  Jun 23 '13 at 21:44

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,

share improve this question
 

1 Answer

up vote 5 down vote accepted

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);
}
share improve this answer
 
   
+1 for clearly explaining why you need that change. An additional note that may or may not be new to the OP: for ordinary functions, it isn't necessarily be a problem if template deduction cannot succeed, as long it's possible to explicitly specify the template arguments. No such syntax exists for templated constructors, so a templated constructor where the template arguments cannot be deduced will never be callable. –   hvd  Jun 23 '13 at 21:44
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值