Portability information in class instance creation

I need to access type information from a class that I used to instantiate another class.

In particular, it void Beta<T>::do_something()must accept the type parameters W, Sthat were used to instantiate the class Alpha<W, S>.

template<typename W, S> 
class Alpha {
public:
  using carry_W = W;
  using carry_S = S;
};

template<typename T> 
class Beta {};
template<typename T>
void Beta<T>::do_something(typename T::carry_W p1, typename T::carry_S p2) {}

Beta<Alpha<int, double>> b;

The solution above works fine, but is there any other way to do this without superimposing types as members of a class? Is there a C ++ way to do this?

+4
source share
2 answers

You can create a class template that consists only of a direct declaration and partial specialization.

#include <iostream>

using namespace std;

template<typename W, typename S> 
class Alpha {
};

template<typename>
class Beta;

template<typename W, typename S, template<typename, typename> class T>
class Beta<T<W,S>> {
public:
  void do_something(W w, S s) {
      cout << w << ", " << s << '\n';
  }
};

int main() { 
    Beta<Alpha<int, double>> b;
    b.do_something(0, 0.0);
}
+1
source

- ( " " )?

template<typename T<W, S>>
void Beta<T>::do_something(W, S) {...}

, , , ++ ...

0

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


All Articles