2

我定义了一个类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 社区版

4

1 回答 1

1

我想你可以解决问题(1)测试!std::is_same_v<T1 const, T2 const>(而不是测试那个T1和不是T2同一类型)的"both view != type"情况(2)使用T1T2(而不是T)在"both view same type"和添加测试std::is_same_v<T1 const, T2 const>

我的意思是(我只有一个 C++14 编译器,所以我使用std::is_same<>::value而不是std::is_same_v<>

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<T1 const, T2 const>::value
      && std::is_same<View1<T1>, view<T1>>::value
      && std::is_same<View2<T2>, view<T2>>::value>
>
 {
   void operator()(View1<T1> const & i_vin, View2<T2> & o_vout) const
    {
      std::cout << "***************" << std::endl;
      std::cout << "both view != type" << 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<T1 const, T2 const>::value
      && std::is_arithmetic<T1>::value
      && std::is_same<View1<T1>, view<T1>>::value
      && std::is_same<View2<T2>, view<T2>>::value>
>
 {
   void operator()(View1<T1> const & i_vin, View2<T2> & o_vout) const
    {
      std::cout << "***************" << std::endl;
      std::cout << "both view same type" << std::endl;
      std::cout << "***************" << std::endl;

    }
 };

顺便说一句:您可以(在您的专业领域中)避免使用and和直接使用,而不是强加View1<T1>and 与 and的View2<T2>类型相同。view<T1>view<T2>View1View2view

您可以按如下方式简化您的专业

template <typename T1, typename T2>
struct PixelWiseUnaryDispatch <T1, T2, view, view,
   std::enable_if_t<!std::is_same<T1 const, T2 const>::value>>
 {
   void operator()(view<T1> const & i_vin, view<T2> & o_vout) const
    {
      std::cout << "***************" << std::endl;
      std::cout << "both view != type" << std::endl;
      std::cout << "***************" << std::endl;
    }
 };
template <typename T1, typename T2>
struct PixelWiseUnaryDispatch<T1, T2, view, view,
   std::enable_if_t<
         std::is_same<T1 const, T2 const>::value
      && std::is_arithmetic<T1>::value>>
 {
   void operator()(view<T1> const & i_vin, view<T2> & o_vout) const
    {
      std::cout << "***************" << std::endl;
      std::cout << "both view same type" << std::endl;
      std::cout << "***************" << std::endl;

    }
 };
于 2017-05-26T01:15:52.827 回答