Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem instantiating struct template

Tags:

c++

templates

How can instantiate this template struct?

template<typename T,
  template<typename, template <typename> class D= std::allocator
           >class Z=std::vector
         >

struct amen
{
    Z<T,D<T> > cc; // i know D template class parameter is not visible , how do i do it?
};

int main(){
amen<int> moreAmen;
}

Can anyone show me how to do it?

like image 478
Mr.Anubis Avatar asked Sep 27 '26 01:09

Mr.Anubis


1 Answers

You have this wrong. It should be

template<typename U, typename D = std::allocator<U>
         > class Z = std::vector

The allocator of std::vector is not a template. Then the declaration of cc becomes

Z<T> cc;

As you gave a default argument for the allocator, you don't need to pass any argument for it. If you wanted to, you would need to pass std::allocator<T> or some other allocator again

Z<T, std::allocator<T> > cc;
// or T<T, my::funny:allocator> cc;

The default argument of a parameter of a template template argument is not "inherited" to the corresponding template template parameter of your class/struct template. You need to specify it again, like above (by stating std::allocator<U> as default template argument).

like image 184
Johannes Schaub - litb Avatar answered Sep 29 '26 15:09

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!