Why is this variation pattern a mistake?

Just wondering why this is not true:

#include <iostream>

template <std::size_t... Is>
void foo(Is&&... args) {
    std::cout << "foo called with " << sizeof...(Is) << "params\n"; 
}

int main() {
    foo(1, 2, 3, 4); 
}

It seems like a perfectly reasonable example, but fails on any compiler I can handle.

If substituted size_tfor class, then the example works as expected. I also tried using the new template parameter auto, but no online compiler accepted this, so I don’t know if this is a wrong use case or a compliance problem.

+4
source share
2 answers

Invalid C ++, therefore.

1, 2, 3, 4, :

void foo(1&&, 2&&, 3&&, 4&&);

.

, , , , , ++ 17:

template<typename T>
  using is_size_t = std::is_same<T, std::size_t>;

template<typename... T>
  std::enable_if_t<std::conjunction<is_size_t<T>...>::value>>
  foo(T&&... args);

, ( ++ 17):

template<typename... T>
  std::enable_if_t<(std::is_same_v<std::size_t, T> && ...)>
  foo(T&&... args);

++ 14 std::conjunction , . and_ p0032r1

+10

. , ( ), , size_t ( ) :

template <class... Is>
    requires (std::is_same<Is, int>::value && ...)
void foo(Is&&... args) { /*...*/ }

(ma fav) ,

template <class T> concept bool Integer = std::is_same<T, int>::value; 

template <Integer... Is> void foo(Is&&... args) { /*...*/ }
//        ^^^^^^^awesome

Live

+2

Source: https://habr.com/ru/post/1666006/


All Articles