Accessing a nested template parameter

I am currently facing an issue with a nested template parameter that I want to access, but I'm not sure if this is possible. Here's a description of the problem:

I have a "Histogram" class from a library that I am not influencing. The class looks like this:

template<int N>
struct Histogram
{
  float histogram[N];
  static int descriptorSize() { return N; }
};

      

Now I want to create my own class for principal component analysis, which should be able to handle histograms of arbitrary sizes:

template<template<typename> class HistogramT> 
class PCA {

  // I'd like to pass on the nested template parameter here
  typedef typename Matrix< float , N , N > MatrixNf;

  // ...
}

      

The final instance of the class will look like this:

PCA<Histogram<153> > pca;

      

Now my question is how, or even if it is possible to access the template parameter N from Histogram<int N>

inside my PCA class?

+3


source to share


3 answers


If you want to do:

PCA<Histogram<153> > pca;

      

Then it PCA

should be:

template <typename Histogram>
class PCA { ... };

      



because it Histogram<153>

is a complete type, not a template. How do I get a number? Just write a dash like:

template <typename T> struct get_histogram_size;

template <int N>
struct get_histogram_size<Histogram<N> > {
    static const int value = N;
}

      

Using:

template <typename Histogram>
class PCA { 
    static const int N = get_histogram_size<Histogram>::value;
    typedef typename Matrix< float , N , N > MatrixNf;
    // ...
};

      

+4


source


template <class T> 
class PCA;

template <int N> 
class PCA<Histogram<N> >
{
    typedef typename Matrix< float , N , N > MatrixNf;
    // ...
};

PCA<Histogram<153> > pca;

      



+3


source


First, the template template parameter is incorrect. He must have int

, not typename

in him

template<template<int> class HistogramT> 
class PCA 

      

But now you cannot write

PCA<Histogram<154>>

      

because it is Histogram<154>

not a template, it is a class. I suggest you just add one more parameter, for example:

template<template<int> class HistogramT, int N> 
class PCA 

      

And use

PCA<Histogram, 154>

      

The bar chart template is not well written. If it were, it would provide something like:

static const int size = N;

      

inside so you can:

 template<class HistT>
    class PCA
    {
        //here, use HistT::size for N.
    }

      

+1


source







All Articles