开发者

C++ typename as variable

开发者 https://www.devze.com 2023-03-22 20:18 出处:网络
Suppose I have a template c开发者_StackOverflow中文版lass MyClass. Is it possible to store the type of the template as an variable of the class? If so how? I\'m curious if it\'s possible to do someth

Suppose I have a template c开发者_StackOverflow中文版lass MyClass. Is it possible to store the type of the template as an variable of the class? If so how? I'm curious if it's possible to do something like this.

Template <typename T> class MyClass;
Template <typename T> class AnotherClass;

MyClass<int> myClassInstance;
AnotherClass<oneInstance.functionThatReturnsTheStoredTypeFromAnInstanceVariableOfMyClass()> anotherClassInstance;

Thanks in advance.


They can be static variables, you can nest typedefs.

template<typename T> class MyClass {
public:
    typedef T type;
};
AnotherClass<MyClass<double>::type> anotherClassInstance;

However, accessing a member variable like you posted is impossible in C++.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号