C ++ templates: compute values ​​and make decisions at compile time

Let's say you have a vector class with the length and type of the template - i.e. vec<2,float>. They can also be nested - vec<2,vec<2,vec<2,float> > >or vec<2,vec<2,float> >. You can calculate how deeply one of these vectors is embedded:

template<typename T>
inline int depth(const T& t) { return 0; }
template<int N, typename T>
inline int depth(const vec<N,T>& v) { return 1+depth(v[0]); }

The problem is that you won’t know how deep it is to runtime, but you may need to know the depth at compile time to do something like this:

// Do this one when depth(v1) > depth(v2)
template<int N, typename T, int M, typename U>
inline vec<N,T> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return v1 + coerce(v2,v1);
}
// Do this one when depth(v1) < depth(v2)
template<int N, typename T, int M, typename U>
inline vec<M,U> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return coerce(v1,v2) + v2;
}

You can't just insert the if statement because (a), which has a deeper effect on the type of return value, and (b) coerce () generates a build error if you try to force a nested vector to a less nested one.

Is it possible to do something like this, or did I push the framework of C ++ templates?

+3
3

. , ,

template<int N, typename T, int M, typename U>
inline typename enable_if<is_deeper<T, U>::value, vec<N,T> >::type 
operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return v1 + coerce(v2,v1);
}

template<int N, typename T, int M, typename U>
inline typename enable_if<is_deeper<U, T>::value, vec<M,U> >::type 
operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
    return coerce(v1,v2) + v2;
}

is_deeper -

/* BTW what do you want to do if none is deeper? */
template<typename T, typename U>
struct is_deeper { static bool const value = false; };

template<typename T, int N, typename U>
struct is_deeper<vec<N, U>, T> { 
  static bool const value = true;
};

template<typename T, int N, typename U>
struct is_deeper<T, vec<N, U> > { 
  static bool const value = false;
};

template<typename T, int N, int M, typename U>
struct is_deeper<vec<M, T>, vec<N, U> > : is_deeper<T, U> 
{ };
+5

. , :

  #include <iostream>
#include <boost\static_assert.hpp>
using namespace std;

template<size_t Depth> class Vec
{
public:
 enum {MyDepth = Vec<Depth-1>::MyDepth + 1};
};

template<> class Vec<1>
{
public:
 enum {MyDepth = 1};
};

  BOOST_STATIC_ASSERT(Vec<12>::MyDepth == 12);
//  Un-commenting the following line will generate a compile-time error
//    BOOST_STATIC_ASSERT(Vec<48>::MyDepth == 12);

int main()
{
 cout << "v12 depth = " << Vec<12>::MyDepth;
}

EDIT: static assert, , .

+3

. inline . (++ 0x , , .)

-, vec boost::array/std::tr1::array/std::array, array.

template< class ArrT >
struct array_depth; // in the general case, array depth is undefined

template< class ElemT, size_t N > // partial specialization
struct array_depth< array< ElemT, N > > { // arrays do have depth
    enum { value = 0 }; // in the general case, it is zero
};

template< class ElemT, size_t N1, size_t N2 > // more specialized than previous
struct array_depth< array< array< ElemT, N1 >, N2 > {
    enum { value = 1 + array_depth< array< ElemT, N1 > >::value }; // recurse
};

// define specializations for other nested datatypes, C-style arrays, etc.
// C++0x std::rank<> already defines this for C-style arrays
+2

Source: https://habr.com/ru/post/1764769/


All Articles