就是这样
typename unwrap<std::vector<int>>::type // int
我这里粗糙的试了一下好像不行
#include <type_traits>
#include <vector>
template <typename T>
struct unwrap {
using type = T;
};
template <typename wrapper, typename T>
struct unwrap<wrapper<T>> {
using type = T;
};
int main(int argc, char* argv[]) {
typename unwrap<std::vector<int>>::type a = 0;
}
1
codehz 2019-11-06 13:18:26 +08:00 via Android
template<template <typename> X, typename T>
就可以匹配模板模板了,不过对于这个例子,vector 的完整形式有两个 typename,所以还要写一个 typename 才能匹配上 |
2
codehz 2019-11-06 13:29:36 +08:00 1
前面少了一个 typename,下面是完整例子
https://gist.github.com/codehz/b18c62d104f2b7f1a4f3bfb44c5d249b |
4
codehz 2019-11-06 14:57:49 +08:00
@shylockhg #3
#include <vector> template <typename T> struct unwrap { using type = T; }; template <template <typename> typename wrapper, typename T> struct unwrap<wrapper<T>> { using type = T; }; template <template <typename, typename> typename wrapper, typename T, typename R> struct unwrap<wrapper<T, R>> { using type = T; }; int main() { typename unwrap<std::vector<int>>::type a = 0; } |