logo
down
shadow

Is it possible for C++ to write a template that takes a Container as parameter?


Is it possible for C++ to write a template that takes a Container as parameter?

By : dotNetDude
Date : November 22 2020, 10:56 AM
this will help You are talking about concepts in C++. The idea is discussed for a long time for now, but they are still not in the standard. See here:
code :
template<Sortable Cont>
void sort(Cont& container);
list<int> lst = ...;   // oops, bidirectional iterators
sort(lst);             // error: 'T' is not a/an 'Sortable' type
std::list<int> l;
std::sort(l.begin(), l.end());
// error C2784: 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<int>>>'
// error C2784: 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<int>>>'
// error C2784: 'unknown-type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<int>>>'


Share : facebook icon twitter icon
Template class that wraps a container of type depending on the template parameter

Template class that wraps a container of type depending on the template parameter


By : jazzFromMars
Date : March 29 2020, 07:55 AM
this one helps. I would like to have a template class that wraps a container, but I would like to make a choice of which container to wrap according to the value of the template parameter. Something like: , You could do:
code :
typedef typename boost::mpl::if_c<
    std::is_same<T, bool>::value,
    std::deque<T>,
    std::vector<T>
>::type MyContainerType;
typedef typename ContainerSelector<T>::type MyContainerType;
template <typename T>
struct ContainerSelector {
    typedef std::vector<T> type;
};

template <>
struct ContainerSelector<bool> {
    typedef std::deque<bool> type;
};
method that takes a class as a template parameter, and takes the parameters of the classes constructor as method paramet

method that takes a class as a template parameter, and takes the parameters of the classes constructor as method paramet


By : user6145473
Date : March 29 2020, 07:55 AM
like below fixes the issue Okay so I'm creating my very own Entity-Component System, and I'm stuck at the AddComponent Entity method, which adds components to the Enity, here is how it looks: , This is the most simplest use case for a parameter pack.
code :
template <typename T, typename ...Args>
void AddComponent(Args && ...args) {
    NumOfComponents++;
    AllComponents.push_back(new T(std::forward<Args>(args)...));
}
How can I write a template function that takes a generic C++ standard library container

How can I write a template function that takes a generic C++ standard library container


By : Jeya Ganesh
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You should declare T as template template parameter to indicate that it's a template-name (and needs arguments to be instantiated), e.g.
code :
template<template <typename...> class T> 
void foo(const T<bar>&, const T<double>&);
Why does this function that takes template template parameter not compile?

Why does this function that takes template template parameter not compile?


By : Shakthi
Date : March 29 2020, 07:55 AM
I hope this helps . You got things too complicated. You should let compiler deduce as much information for you as possible and instead of writing
How to write the constructor for a template class that wraps a container where the container can be both an array or a v

How to write the constructor for a template class that wraps a container where the container can be both an array or a v


By : Augnelli
Date : March 29 2020, 07:55 AM
will help you What about using old C-style arrays and delegating constructors?
Something as follows
code :
  template <typename T, std::size_t N, std::size_t ... Is>
  Sum (T const (& il)[N], std::index_sequence<Is...> const)
        : m_terms{{il[Is]...}}
   { }

  template <typename T, std::size_t N>
  Sum (T const (& il)[N]) : Sum(il, std::make_index_sequence<N>{})
   { }
struct MySum : Sum<std::vector<int>>
{
  MySum(int t0, int t1, int t2, int t3) : Sum<std::vector<int>>{{t0, t1, t2, t3}} { }
}; // ..........................................................^..............^

struct MySum4 : Sum<std::array<int, 4>>
{
  MySum4(int t0, int t1, int t2, int t3) : Sum<std::array<int, 4>>{{t0, t1, t2, t3}} { }
}; // .............................................................^..............^
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org