我定义了一个类array_view和一个类strided_view(想想array_view和strided_array_view http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0122r0.pdf),我想专门研究我可以对其进行迭代以提高效率的方式假设我有一个函数调度程序,它试图专门处理不同的情况。
让我们从一些简单的代码开始
template <class T>
class view
{};
template <class T>
class sview
{};
template<typename T1, typename T2, template <typename> class View1, template <typename> class View2>
void PixelWiseUnary(const View1<T1>& i_vin, View2<T2>& o_vout)
{
PixelWiseUnaryDispatch<T1, T2, View1, View2> dispatcher;
dispatcher(i_vin, o_vout);
}
然后我定义不同的专业
// primary use strided view
template<typename T1, typename T2, template <typename> class View1, template <typename> class View2, typename = void>
struct PixelWiseUnaryDispatch
{
void operator()(const View1<T1>& i_vin, View2<T2>& o_vout) const
{
std::cout << "***************" << std::endl;
std::cout << "primary template" << std::endl;
std::cout << "***************" << std::endl;
}
};
template<typename T1, typename T2, template <typename> class View1, template <typename> class View2>
struct PixelWiseUnaryDispatch<T1,T2, View1, View2,
std::enable_if_t<(!std::is_same_v<T1,T2> && std::is_same_v<View1<T1>,view<T1>> )&& std::is_same_v<View2<T2>, view<T2>>>
>
{
void operator()(const View1<T1>& i_vin, View2<T2>& o_vout) const
{
std::cout << "***************" << std::endl;
std::cout << "both view != type" << std::endl;
std::cout << "***************" << std::endl;
}
};
template<typename T,template <typename> class View1, template <typename> class View2>
struct PixelWiseUnaryDispatch<T,T, View1, View2,
std::enable_if_t<(std::is_arithmetic_v<T> && std::is_same_v<View1<T>, view<T>>) && std::is_same_v<View2<T>, view<T>>>
>
{
void operator()(const View1<T>& i_vin, View2<T>& o_vout) const
{
std::cout << "***************" << std::endl;
std::cout << "both view same type" << std::endl;
std::cout << "***************" << std::endl;
}
};
然后定义一个简单的 main
void main(void)
{
view<int> vin;
view<float> vinf;
view<int> vout;
sview<int> vsout;
PixelWiseUnary(vin, vsout); //primary template
PixelWiseUnary(vinf, vout); //both view != type
PixelWiseUnary(vin, vout); //both view same type
}
一切都很好并正确切换
但是当我尝试使用 const Eg 时事情变得很奇怪
void main(void)
{
view<const int> vin;
view<const float> vinf;
view<int> vout;
sview<int> vsout;
PixelWiseUnary(vin, vsout); //primary template as expected
PixelWiseUnary(vinf, vout); //both view != type WTF i don't provide specialisation for const (cf https://stackoverflow.com/questions/14926482/const-and-non-const-template-specialization) so i expected primary template
PixelWiseUnary(vin, vout); //both view != type WTF i don't provide specialisation for const and i loose the same type specialization
}
}
我尝试添加来自Const 和非 const 模板专业化的建议,但在我的情况下它没有任何改变。
我错过了什么?问候
注意:我使用最新的 Visual2017 社区版