C++ 使用返回的模板参数创建对象失败,原因是什么?
我想使用一个类中的模板参数来实例化具有相同模板参数的另一个对象。 我试图将参数保存在结构中,但无法将该类型用于新对象C++ 使用返回的模板参数创建对象失败,原因是什么?,c++,templates,C++,Templates,我想使用一个类中的模板参数来实例化具有相同模板参数的另一个对象。 我试图将参数保存在结构中,但无法将该类型用于新对象 template<typename D>struct Typename { using myType = D; }; template<typename T> class example{ public: Typename<T> someType; }; int main(){ example<dou
template<typename D>struct Typename {
using myType = D;
};
template<typename T>
class example{
public:
Typename<T> someType;
};
int main(){
example<double> e1;//ok
example<e1.someType.myType> e2; //error: cannot refer to member 'myType'
}
templatestruct类型名{
使用myType=D;
};
模板
课例{
公众:
Typename someType;
};
int main(){
示例e1;//确定
示例e2;//错误:无法引用成员“myType”
}
错误消息:
error: invalid use of ‘using myType = double’
example<e1.someType.myType> e2;
^~~~~~
error: template argument 1 is invalid
example<e1.someType.myType> e2;
^
error: conflicting declaration ‘example<double> e2’
example<typename decltype(e1.someType)::myType> e2; //error: cannot refer to member 'myType'
^~
错误:“using myType=double”的使用无效
示例e2;
^~~~~~
错误:模板参数1无效
示例e2;
^
错误:声明“示例e2”冲突
示例e2//错误:无法引用成员“myType”
^~
myType
不是Typename
的数据成员,因此不能使用成员访问语法(
)。您需要使用范围解析运算符(:
)从类型本身访问myType
。例如
example<decltype(e1.someType)::myType>
示例
在此处使用decltype
。。