基本上,我想做的是这样的:
template<typename x,typename y,typename z>
struct SomeTemplateClass {
typedef x myType;
};
然后...
SomeTemplateClass<LongVariableNames,ILikePie,AndCheese> cheese;
cheese.myType i;
因此 i
将具有 LongVariableNames
类型。
我知道做这样的事情可能是不好的做法,但我只是想知道。执行此类操作的最简单方法是什么?
我的意思是,由于强静态类型等原因,应该可以在编译时轻松解决此问题。像 sizeof()
这样的 typeof()
会很有用。
最佳答案
一般是这样的:
typedef SomeTemplateClass<LongVariableNames,ILikePie,AndCheese> foodType;
foodType cheese;
foodType::myType i;
在 C++11 中,您还可以使用 decltype(cheese)::myType
,但这非常难看。
关于c++ - 将 typedef 作为类的非静态成员访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8235142/