mercredi 8 janvier 2020

How to use a type of typename?

I'm new to C++11 and trying to write a function able to handle dynamic types.

#include <functional>
#include <stdio.h>

template <typename T>
struct scase {
  T param;
  std::function<void(T &&)> pc;
  // typedef T type;
  // or
  // using type = T;
  // ?
};

template <size_t I, typename... T>
void docase(std::tuple<T...> &t) {
  auto sc = std::get<I>(t);

  // using casetype = typename std::tuple_element<I, std::tuple<T...>>::type;
  // typename casetype::type ti;
  // ti = sc.param;
  // sc.pc(ti);
}

template <typename... T>
void select(T &&... cases) {
  auto tuple = std::forward_as_tuple(cases...);
  docase<0>(tuple);
}

int main() {
   select(
     scase<int>{123, [](int &&v) {
       printf("%d\n", v);
     }
   )
   return 0;
}

The commented codes cause compiler errors. What's the correct syntax for this?

Edit:

errors are like:

error C2825: 'casetype': must be a class or namespace when followed by '::'

error C2510: 'casetype': left of '::' must be a class/struct/union

error C2065: 'type': undeclared identifier

error C2146: syntax error: missing ';' before identifier 'ti'

error C2065: 'ti': undeclared identifier

Aucun commentaire:

Enregistrer un commentaire