2017-07-25 277 views
4

假设std::vector没有value_type。是可以写出一个模板来推导出value_type?或者更一般的,给出T<X>,我怎么能推导出X如何获取模板模板参数的模板参数?

很天真..

template <template <typename X> T> 
void test(T<X> t) { 
    X x; 
} 

将可能使任何人谁知道一点关于模板嘲笑我的愚蠢的尝试,当实例化这样的:

int main() { 
    std::vector<int> x; 
    test(x); 
} 

创建以下错误:

error: expected ‘class’ before ‘T’ 
template < template<typename X> T> 
           ^
error: ‘X’ was not declared in this scope 
void test(T<X> u) { 
      ^
error: template argument 1 is invalid 
void test(T<X> u) { 
      ^
In function ‘void test(int)’: 
error: ‘X’ was not declared in this scope 
    X x; 
^
error: expected ‘;’ before ‘x’ 
    X x; 
    ^
In function ‘int main()’: 
error: no matching function for call to ‘test(std::vector<int>&)’ 
    test(x); 
     ^
note: candidate is: 
note: template<template<class X> class T> void test(int) 
void test(T<X> u) { 
    ^
note: template argument deduction/substitution failed: 
note: cannot convert ‘x’ (type ‘std::vector<int>’) to type ‘int’ 

编辑:第一个很容易修复,但修复它不会影响o thers ...

PS:我想我有一个小小的误解,因为std::vector<int>不是一个模板,而是一个具体的类型。不过,我仍然想知道是否有办法从someTemplate<int>获得int以及一些模板魔法。

+0

如果我记得正确的,你必须在嵌套模板中使用'class'。类似于'template