-1

Basically I need template function, that would compare container's size with some constant. So I need to make std::vector of containers and check if predicate compSize is true.

template < int x, class Container >
bool compSize(Container cont)
{
    return x == cont.size();
}
    
template < int x, class ...Container >
bool compSizeMult(Container ...conts)
{
    std::vector< Container > cvector{ conts... };
    return std::all_of(cvector.cbegin(), cvector.cend(), compSize< x, Container >);
}

But compiler says, that parameter pack is not expanded at

cvector{ conts... };

Also I would like to use compSizeMult as comparator for std::sort and make something like

int main()
{
    std::vector< std::vector< int > > vec{{1}, {2}, {3}, {0}, {0}};
    std::sort(vec.begin(), vec.end(), compSizeMult< 0, std::vector< int > >);
}

that's why function is supposed to take multiple arguments. I can't use loops and lambdas in this task, only standard algorithms

aaletov
  • 1
  • 1
  • Standard says that it is permissible to use variadic template in initializer list. Other answers contain code snippets, where variadic template is used in such way. But... It's just doesn't work – aaletov Jun 02 '21 at 03:32
  • I've just realized that I can't use this function as comparator – aaletov Jun 02 '21 at 03:44

1 Answers1

0

You have 2 variadics in

std::vector<Container> cvector{ conts... };
  • variable conts which have its ...,
  • Type Container which doesn't have its ... (neither in compSize<x, Container>).

You might want std::common_type_t<Container...>.

In C++17, you might use Fold expressions:

template < int x, class ... Containers >
bool compSizeMult(Containers&&... conts)
{
    return (compSize<x>(conts) && ...);
    // or directly: return (conts.size == x && ...);
}

I would like to use compSizeMult as comparator

It is not a valid comparer. It breaks strict weak ordering.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • So I can't unpack conts into vector? – aaletov Jun 02 '21 at 11:39
  • `std::vector> cvector{ conts... };` should work (if there is a common type, you cannot mix `std::vector` with `std::list` with std::vector`). Alternatively, you can `std::vector sizes{conts.size()...};` – Jarod42 Jun 02 '21 at 12:38