Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板:数组大小扣除失败_C++_Templates_Iterator - Fatal编程技术网

C++ 模板:数组大小扣除失败

C++ 模板:数组大小扣除失败,c++,templates,iterator,C++,Templates,Iterator,我试图将要处理的元素的类型限制为std::array>,但模板替换N失败 main.cpp:10:1: note: template argument deduction/substitution failed: main.cpp:31:34: note: couldn't deduce template parameter 'N' print(word.begin(),word.end()); 我的尝试如下 #include <iostream> #includ

我试图将要处理的元素的类型限制为
std::array>
,但模板替换N失败

main.cpp:10:1: note:   template argument deduction/substitution failed:
main.cpp:31:34: note:   couldn't deduce template parameter 'N'
     print(word.begin(),word.end());
我的尝试如下

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <boost/range/iterator_range.hpp>    

template<typename ForwardIterator,std::size_t N>
typename std::enable_if<std::is_same<typename ForwardIterator::value_type,std::array<std::string,N> >::value >::type
print(ForwardIterator begin, ForwardIterator end)
{
    for( auto const & arr : boost::make_iterator_range(begin,end) )
    {
        for( auto const & item : arr)
        {
            std::cout<<item<<' ';
        }
        std::cout<<'\n';
    }
}

int main()
{
    std::vector<std::array<std::string,2>> word;
    word.push_back( {{"hello","Hi"}} );
    word.push_back( {{"b","bye"}} );
    print(word.begin(),word.end());
}
#包括
#包括
#包括

#include

您必须更改方法,并验证类型本身是否可以推断为数组,而不是将类型与未知大小的数组进行比较(也就是说,编译器无法判断
N
将考虑什么):

#include <array>
#include <iterator>
#include <type_traits>
#include <string>

template <typename T, typename A>
struct is_std_array : std::false_type {};

template <typename T, std::size_t N>
struct is_std_array<T, std::array<T,N>> : std::true_type {};

template<typename ForwardIterator>
auto print(ForwardIterator begin, ForwardIterator end)
    -> typename std::enable_if<
           is_std_array<std::string, typename std::iterator_traits<ForwardIterator>::value_type>::value
       >::type
{

}

#include

您必须改变方法,而不是将类型与未知大小的数组进行比较(也就是说,编译器无法判断
N
会想到什么),而是验证类型本身是否可以推断为数组:

#include <array>
#include <iterator>
#include <type_traits>
#include <string>

template <typename T, typename A>
struct is_std_array : std::false_type {};

template <typename T, std::size_t N>
struct is_std_array<T, std::array<T,N>> : std::true_type {};

template<typename ForwardIterator>
auto print(ForwardIterator begin, ForwardIterator end)
    -> typename std::enable_if<
           is_std_array<std::string, typename std::iterator_traits<ForwardIterator>::value_type>::value
       >::type
{

}
#include

(这并不是一个完整的答案,只是试图直观地解释当前方法不起作用的原因。)

这是因为你没有给它任何推断的机会。
N
在签名中只出现一次,这是为了测试是否相等。这并不强制类型彼此相等

作为一个非常简单的示例,您不会期望此代码将
t
强制为
int

template<typename T>
typename std::enable_if<std::is_same<T,int >::type
void foo(T) {
}
模板
typename std::enable_if(这并不是一个完整的答案,只是试图直观地解释当前方法不起作用的原因。)

这是因为你没有给它任何推断的机会。
N
在签名中只出现一次,这是为了测试是否相等。这并不强制类型彼此相等

作为一个非常简单的示例,您不会期望此代码将
t
强制为
int

template<typename T>
typename std::enable_if<std::is_same<T,int >::type
void foo(T) {
}
模板

typename std::enable_如果您是正确的
我想确保仅使用值类型为std::array的迭代器调用它
是的您是正确的
我想确保仅使用值类型为std::array的迭代器调用它