Creating a variable template type from another variable template type

If I have a variable whose type was a template, and I want to create another variable of another template, but use the same template parameter, how can I?

While the example below does not work, it hopefully gives an idea of ​​what I want to accomplish. If there is no particular way to do this, is there a way to create some sort of lookup table design template? There are only six different types of patterns that I probably end up using.

template<typename T>
class A{
public:
    typedef T Type;
};

template<typename T>
class B{};

int main(void){
    A<int> var1;

    B<var1::Type> var2;
}

My apologies if this is a duplicate question, but I have not seen anything like it before.

EDIT

I would like this to work with Visual Studio 2010 and gcc without using a third-party library. Therefore, not all C ++ 11 features can be supported.

, typedef A<int> A<int>, , , . , , .

+3
2

V++ 2010, , - :

template<typename T>
struct A {
    typedef T Type;
    Type foo();
};

template<typename T>
struct B {};

int main() {
    A<int> var1;
    B<decltype(var1.foo())> var2;
}

, .

+2

++ 11 decltype:

B<decltype(var1)::Type> var2;

. . decltype .

- ++ 03, :

template <typename T>
void doSomething(const T& var1) {

  B<typename T::Type> var2;
  // do whatever you wanted to do with var2 here.
  // problem is, you cannot return it.
  std::cout << "Doing something!\n";

}

int main(void){
    A<int> var1;
    doSomething(var1);
}

: @MSalters, , VS2010 auto, :

template <typename T>
B<typename T::Type> makeB(const T&) {
  return B<typename T::Type>();
}

:

int main(void){
    A<int> var1;
    auto var2 = makeB(var1);
}
+2

All Articles