Vinicius Miranda
Vinicius Miranda

Reputation: 1

Template parameter to vector::constructor

I am trying to code a small C++ reimplementation of GSL integraton routines as a practice project to learn C++ metaprogramming. I have the following question.

I've define some type traits (to make the program to work both with double and floats)

template<typename T> class IntegrationWorkspaceTraits;

template<> class IntegrationWorkspaceTraits<double>
{
     public:
     typedef double ft; //float_type
     static constexpr ft zero = 0.0;
}; 

template<> class IntegrationWorkspaceTraits<float>
{
    public:
    typedef float ft; //float_type
    static constexpr ft zero = 0.0f;
}; 

And now I have a class that uses this traits that look like this

template< typename T, typename AT = IntegrationWorkspaceTraits<T> >    GslIntegrationWorkspace
{
    typedef typename AT::ft   ft;
    typedef typename AT::zero zero;

    public:
    GslIntegrationWorkspace(size_t size);

    private:
    typename std::vector<ft> alist;
}

My question is: How to use the zero constant that is defined on traits in order to set the initial value of the member vector. My guess is something like

template<typename T, typename AT> 
GslIntegrationWorkspace::GslIntegrationWorkspace( size_t size ):
alist(size, typename AT::zero),   
{};

But compiler g++ complains "gsl_integration.h:63:42: error: invalid use of template-name ‘GslIntegrationWorkspace’ without an argument list"

best

Upvotes: 0

Views: 209

Answers (2)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153820

You need to implement your constructor like this:

template<typename T, typename AT> 
GslIntegrationWorkspace<T, AT>::GslIntegrationWorkspace( size_t size ):
    alist(size, AT::zero),   
{
}

Your post was also missing a class when defining GslIntegrationWorkspace.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476990

zero is a value, not a type! You need this:

typedef typename AT::ft ft;
static constexpr ft     zero = AT::zero;

Now you can use GslIntegrationWorkspace<double>::zero, etc. In the constructor you would of course just need alist(size, zero).

If you don't ODR-use the value (e.g. take its address), you won't even need to have a definition for it - the inline declaration and initialization should suffice.

Upvotes: 1

Related Questions