Preventing compilation of conversion statements if a static condition is not met

I have the Vector ( CVector<T, std::size_t Size>), Matrix ( CMatrix<T, std::size_t Height, std::size_t Width>), and Tensor ( CTensor<T, std::size_t... Sizes>) classes , and I want to be able to implicitly convert from class CTensorto class CVector, if to sizeof...(Sizes) == 1class CMatrix, if sizeof...(Sizes) == 2, therefore, I have the following conversion operators (initially I did not have a template parameter std::enable_ifin in the hope that I can use SFINAE so that it does not compile):

template <typename std::enable_if<sizeof...(Sizes) == 2, int>::type = 0>
operator CMatrix<NumType, Sizes...>() const
{
    static_assert(sizeof...(Sizes) == 2, "You can only convert a rank 2 tensor to a matrix");

    CMatrix<NumType, Sizes...> matResult;

    auto& arrThis = m_numArray;
    auto& arrResult = matResult.m_numArray;
    concurrency::parallel_for_each( arrResult.extent, [=, &arrThis, &arrResult]( concurrency::index<2> index ) restrict( amp ) {
        arrResult[index] = arrThis[index];
    } );

    return matResult;
}

template <typename std::enable_if<sizeof...(Sizes) == 1, int>::type = 0>
operator CVector<NumType, Sizes...>() const
{
    static_assert(sizeof...(Sizes) == 1, "You can only convert a rank 1 tensor to a vector");

    CVector<NumType, Sizes...> vecResult;

    auto& arrThis = m_numArray;
    auto& arrResult = vecResult.m_numArray;
    concurrency::parallel_for_each( arrResult.extent, [=, &arrThis, &arrResult]( concurrency::index<1> index ) restrict( amp ) {
        arrResult[index] = arrThis[index];
    } );

    return vecResult;
}

, CTensor<float, 3, 3, 3>, , , , , CMatrix CVector, std::enable_if<false, int>. CTensor 1 2?

+4
4

, .

SFINAE , static_assert , .

( , ) -1, 1 ( , , ), , .

.

#include <cstddef>

template <typename T, unsigned int index, T In, T... args>
struct GetArg
{
    static const T value = GetArg<T, index-1, args...>::value;
};

template <typename T, T In, T... args>
struct GetArg<T, 0, In, args...>
{
    static const T value = In;
};

template <typename T, T In>
struct GetArg<T, 1, In>
{
    static const T value = -1;
};

template <typename T, std::size_t Size>
struct CVector
{
};

template <typename T, std::size_t Height, std::size_t Width>
struct CMatrix
{
};

template <typename T, std::size_t... Sizes>
struct CTensor 
{
    template <std::size_t SZ = sizeof...(Sizes)>
    operator CVector<T, GetArg<std::size_t, 0, Sizes...>::value>() const
    {
        static_assert(SZ == 1, "You can only convert a rank 1 tensor to a vector");
        CVector<T, Sizes...> vecResult;
        return vecResult;
    }

    template <std::size_t SZ = sizeof...(Sizes)>
    operator CMatrix<T, GetArg<std::size_t, 0, Sizes...>::value, GetArg<std::size_t, 1, Sizes...>::value>() const
    {
        static_assert(SZ == 2, "You can only convert a rank 2 tensor to a matrix");
        CMatrix<T, Sizes...> matResult;
        return matResult;
    }
};

int main()
{
    CTensor<float, 3> tensor3;
    CTensor<float, 3, 3> tensor3_3;
    CTensor<float, 3, 3, 3> tensor3_3_3;
    CVector<float, 3> vec(tensor3);
    //CVector<float, 3> vec2(tensor3_3); // static_assert fails!
    CMatrix<float, 3, 3> mat(tensor3_3);
    //CMatrix<float, 3, 3> mat2(tensor3_3_3); // static_assert fails!
}
+3

static_assert:

template <typename NumType,size_t... Sizes>
struct CTensor {
    template<size_t n,size_t m>
    operator CMatrix<NumType,n,m>() const
    {
        static_assert(
            sizeof...(Sizes)==2,
            "You can only convert a rank 2 tensor to a matrix"
        );
        static_assert(
            std::is_same<CTensor<NumType,n,m>,CTensor>::value,
            "Size mismatch"
        );

        ...    
    }

    template<size_t n>
    operator CVector<NumType,n>() const
    {
        static_assert(
            sizeof...(Sizes)==1,
            "You can only convert a rank 1 tensor to a vector"
        );
        static_assert(
            std::is_same<CTensor<NumType,n>,CTensor>::value,
            "Size mismatch"
        );

        ...    
    }
};

SFINAE:

template <typename NumType,size_t... Sizes>
struct CTensor {
    template<size_t n,size_t m,
      typename =
        typename std::enable_if<
          std::is_same<CTensor<NumType,n,m>,CTensor>::value, int
        >::type
    >
    operator CMatrix<NumType,n,m>() const
    {
        ...
    }

    template<size_t n,
      typename =
        typename std::enable_if<
          std::is_same<CTensor<NumType,n>,CTensor>::value, int
        >::type
    >
    operator CVector<NumType,n>() const
    {
        ...
    }
};

:

template <typename NumType,size_t... Sizes>
struct CTensor {
    template<size_t n,size_t m>
    CMatrix<NumType,n,m> convert() const
    {
        ...
    }

    template<size_t n>
    CVector<NumType,n> convert() const
    {
        ...
    }

    template <typename T>
    operator T() const { return convert<Sizes...>(); }
};
+2

: CTensor CVector/CMatrix? , .
... -, . :)


1) detail
2) , ( , , )
3) CVector/CMatrix CTensor ( )

#include <vector>

namespace detail {

template<class T, std::size_t... Sizes>
  class base;
template<class T, std::size_t Size>
  class base<T, Size> {
    std::vector<T> data;
public:
    T& operator[](std::size_t i) {
        return data[i]; }
};
template<class T, std::size_t First, std::size_t... More>
  class base<T, First, More...> {
    std::vector<base<T, More...>> data;
public:
//  this could be done better, just an example
    base<T, More...>& operator[](std::size_t i) {
        return data[i]; }
};

}

template<class T, std::size_t... Sizes>
  class CTensor: public detail::base<T, Sizes...> {};
//we can specialize CTensor<T, Size>
//and CTensor<T, Width, Height> here
template<class T, std::size_t Size>
  using CVector = CTensor<T, Size>;
template<class T, std::size_t Width, std::size_t Height>
  using CMatrix = CTensor<T, Width, Height>;
+1

Make the sizeof...(Sizes)dependent argument and make the correct type CMatrix/ CVector(by taking the correct number of template parameters).

Using:

template <std::size_t ... Is> struct index_sequence {};

template <std::size_t I, typename T> struct index_element;

template <std::size_t I, std::size_t ... Is>
struct index_element<I, index_sequence<Is...> >
{
private:
    static constexpr const std::size_t a[] = {Is...};
public:
    static_assert(I < sizeof...(Is), "out of bound");
    static constexpr const std::size_t value = a[I];
};

then you can do:

template <
    std::size_t N = sizeof...(Sizes),
    typename std::enable_if<N == 1, int>::type = 0>
operator CVector<
    T,
    index_element<0, index_sequence<Sizes..., 0>
    >::value>() const
{
    // Your implementation
}

template <
    std::size_t N = sizeof...(Sizes),
    typename std::enable_if<N == 2, int>::type = 0>
operator CMatrix<
    T,
    index_element<0, index_sequence<Sizes..., 0>>::value
    index_element<1, index_sequence<Sizes..., 0, 0>>::value
    >() const
{
    // Your implementation
}
0
source

All Articles