是否可以在C ++中匹配递归整数模板参数? [英] Is it possible to match recursively integer template parameters in C++?
问题描述
我有以下问题.我这样定义了N维向量
I have the following problem. I define a N dimensional vector as so
#include <vector>
#include <utility>
#include <string>
template <int N, typename T>
struct NVector{
typedef std::vector<typename NVector<N-1,T>::type> type;
};
template <typename T> struct NVector<1,T> {
typedef std::vector<T> type;
};
我希望编写一个高阶函数 Map ,该函数可以转换嵌套向量的叶元素,无论深度如何,并返回相同形状的新嵌套向量.我尝试过
I wish to write a higher order function Map that can transform the leaf elements of the nested vector no matter how deep and return a new nested vector of the same shape. I have tried
template <int N, typename T, typename Mapper>
struct MapResult {
typedef decltype ( (std::declval<Mapper>()) (std::declval<T>()) ) basic_type;
typedef typename NVector<N, basic_type>::type vector_type;
};
template <int N, typename T, typename Mapper>
typename MapResult<N,T,Mapper>::vector_type
Map( typename NVector<N,T>::type const & vector, Mapper mapper)
{
typename MapResult<N,T,Mapper>::vector_type out;
for(auto i = vector.begin(); i != vector.end(); i++){
out.push_back(Map(*i,mapper));
}
return out;
}
template <typename T, typename Mapper>
typename MapResult<1,T,Mapper>::vector_type
Map(typename NVector<1,T>::type const & vector, Mapper mapper)
{
typename MapResult<1,T,Mapper>::vector_type out;
for(auto i = vector.begin(); i != vector.end(); i++){
out.push_back(mapper(*i));
}
return out;
}
然后像main一样使用它
and then use it in main like
int main(){
NVector<1,int>::type a = {1,2,3,4};
NVector<2,int>::type b = {{1,2},{3,4}};
NVector<1,std::string>::type as = Map(a,[](int x){return std::to_string(x);});
NVector<2,std::string>::type bs = Map(b,[](int x){return std::to_string(x);});
}
但是我遇到编译错误
<source>:48:34: error: no matching function for call to 'Map'
NVector<1,double>::type da = Map(a,[](int x)->int{return (double)x;});
^~~
<source>:20:5: note: candidate template ignored: couldn't infer template argument 'N'
Map( typename NVector<N,T>::type const & vector, Mapper mapper)
^
<source>:31:5: note: candidate template ignored: couldn't infer template argument 'T'
Map(typename NVector<1,T>::type const & vector, Mapper mapper)
^
<source>:49:34: error: no matching function for call to 'Map'
NVector<2,double>::type db = Map(b,[](int x)->int{return (double)x;});
^~~
<source>:20:5: note: candidate template ignored: couldn't infer template argument 'N'
Map( typename NVector<N,T>::type const & vector, Mapper mapper)
^
<source>:31:5: note: candidate template ignored: couldn't infer template argument 'T'
Map(typename NVector<1,T>::type const & vector, Mapper mapper)
^
2 errors generated.
Compiler returned: 1
我猜测编译器不够聪明(或标准未指定),无法通过推导找出参数N.有什么办法可以做到这一点?
I'm guessing that the compiler isn't smart enough ( or the standard doesn't specify how ) to figure out the parameter N by deduction. Is there a way I can achieve this?
我以前是通过std :: vector派生的,但是实际上是通过另一种方式,但是我不喜欢这种解决方案,因为它可以与当前现有的代码一起使用而不必引入新的包装器类型是一件很不错的事情
I previously had this working but in a different way by actually deriving from std::vector but I don't like this solution as it would be nice to have it work with currently existing code without having to introduce a new wrapper type.
/// define recursive case
template <int N, typename T>
struct NVector : std::vector<NVector<N-1,T>>;
/// define termination case
template <typename T>
struct NVector<1, T> : public std::vector<T>;
位于 https://godbolt.org/z/AMxpuj
推荐答案
您不能从typedef推论得出,特别是在帮助器类中声明的typedef,因为编译器无法从类型执行反向映射到参数组合.
You can't deduce from a typedef - especially a typedef declared within a helper class - because there's no way for the compiler to perform the reverse mapping from a type to combinations of arguments.
(考虑到一般情况下这是不可能的,因为有人可能会专门化 struct NVector< 100,float> {使用type = std :: vector< char> ;;};
,并且编译器具有无法知道这是否有意.)
(Consider that in the general case this is impossible since someone might specialize struct NVector<100, float> { using type = std::vector<char>; };
, and the compiler has no way to know whether this is intended.)
为帮助编译器,您可以定义反向映射:
To help the compiler out you could define the reverse mapping:
template<class T> struct NVT { static constexpr auto D = 0; using V = T; };
template<class T> struct NVT<std::vector<T>> : NVT<T> {
static constexpr auto D = NVT<T>::D + 1;
};
可能的用法(C ++ 17,但是足够容易才能翻译成古方言):
Possible usage (C++17, but it's easy enough to translate to archaic dialects):
template<class NV, class Mapper>
auto Map(NV const& vector, Mapper mapper) {
static constexpr auto N = NVT<NV>::D;
using T = typename NVT<NV>::V;
if constexpr (N == 0)
return mapper(vector);
else
{
typename MapResult<N,T,Mapper>::vector_type out;
for (auto const& x : vector)
out.push_back(Map(x, mapper));
return out;
}
}
这篇关于是否可以在C ++中匹配递归整数模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!